• Mysql与PostgreSQL时间问题


    PostgreSQL 中的单引号与双引号

    PostgreSQL 中的单引号与双引号
    在pg中的sql,单引号用来标识实际的值,双引号用来标识表名(table name)或列名(column name)等数据库中存在的值。
    如,执行一句query:

    select "name" from "students" where "id"='1' 
    

    PostgreSQL的时间问题

    使用datetime.datetime.now()插入数据库中的值是东八区时间,例如:2020-04-21 15:24:21.316479+08  

    当前遇到两种情况,配置一样均为RPC:

    1、所有时间参数都为东八区,返回的数据也是东八区

    import   random
    import psycopg2
    import  datetime
    conn = psycopg2.connect(database="bms", user="bms", password="bms@2018", host="192.168.99.200", port="5432")
    cursor=conn.cursor()
    began_time = "2020-04-21 14:00:00"
    end_time = "2020-04-21 17:00:00"
    sql = """
    select  bi_aom,timestamp from  tb_2b701b5a704e4e3cb53be00e751bd26d  where timestamp  between  '{}' and  '{}'
    """.format(began_time,end_time)
    print(sql)
    # sql = """
    # INSERT INTO "public"."tb_2b701b5a704e4e3cb53be00e751bd26d"("bi_pwr", "ai_frq", "ao_frq", "bo_onf", "bi_onf", "bi_aom", "bi_wfs", "bi_fault", "device_uuid", "timestamp") VALUES (0, NULL, NULL, 0, 0, 1, 0, 0, NULL, '%s');
    #
    # """%(datetime.datetime.now())
    cursor.execute(sql)
    
    data  = cursor.fetchall()
    print(data)
    

    2、另一种情况,数据库保存的为东八区时间,但是传入查询时间需要是utc,也就是减去八小时,返回给我时间也是utc则需要加8小时处理

     #传入时间减去八小时
     begin_time = (datetime.datetime.strptime(begin_time, "%Y-%m-%d %H:%M:%S") - datetime.timedelta(hours=8)).strftime("%Y-%m-%d %H:%M:%S")
     end_time = (datetime.datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S")- datetime.timedelta(hours=8)).strftime("%Y-%m-%d %H:%M:%S")
     #查找pg数据
     cursor = connections['timescale'].cursor()
     #pg_sql = "SELECT mean_temperature, to_char(TIMESTAMP, 'YYYY-MM-DD HH24:MI:SS') from {} WHERE TIMESTAMP BETWEEN '{}'  and '{}' ORDER BY TIMESTAMP".format(uuid, begin_time, end_time)
     pg_sql = "SELECT mean_temperature,to_char(TIMESTAMP + '8 hour'  ,'YYYY-MM-DD HH24:MI:SS') from {} WHERE timestamp BETWEEN '{}'  and '{}' ORDER BY TIMESTAMP".format(uuid, begin_time, end_time)
     print(pg_sql)
     cursor.execute(pg_sql)
     result = cursor.fetchall()  

    具体原因未知

  • 相关阅读:
    spark shuffle过程分析
    Android实现网络多线程断点续传下载
    幻世(OurDream)TM 2D图形引擎开通捐赠渠道
    MDA模型定义及扩展
    STL在迭代的过程中,删除指定的元素
    c# POST和GET方式通过server地址提交数据
    Python爬虫抓取csdn博客
    Word Ladder II
    HDU 4183 Pahom on Water(最大流SAP)
    poj1011 Sticks
  • 原文地址:https://www.cnblogs.com/weidaijie/p/12745623.html
Copyright © 2020-2023  润新知