python访问TimeScaleDB
2020-02-02 17:12
459 查看
封装了一个通过python访问TimeScaleDB的类,代码如下:
# 导入依赖包 # !/usr/bin/python3 import time import datetime import psycopg2 class TimeScaleDBVisiter(): def __init__(self): self.beginTime = datetime.datetime.now() #microsecond = self.beginTime + datetime.timedelta(microseconds=1000100) #-- Do not forget to create timescaledb extension #CREATE EXTENSION timescaledb; # 创建表 # cur.execute("CREATE TABLE student(id integer,name varchar,sex varchar);") strCreateTable = "CREATE TABLE conditions ( \ time TIMESTAMPTZ NOT NULL, \ location TEXT NOT NULL, \ temperature DOUBLE PRECISION NULL, \ humidity DOUBLE PRECISION NULL \ );" strCreateTable = "CREATE TABLE AllPulses ( \ id integer NOT NULL, \ rd integer NOT NULL, \ paw integer NOT NULL, \ soa TIMESTAMPTZ NOT NULL, \ inp integer NOT NULL, \ moa integer NOT NULL, \ pan integer NOT NULL \ );" #-- Then we convert it into a hypertable that is partitioned by time #SELECT create_hypertable('conditions', 'time'); def Init(self): # 创建连接对象 self.conn = psycopg2.connect(database="postgres", user="postgres", password="Pswiee2006*", host="localhost", port="5432") self.cur = self.conn.cursor() # 创建指针对象 def SaveInputs(self, dictInput): # 插入数据 #cur.execute("INSERT INTO student(id,name,sex)VALUES(%s,%s,%s)", (1, 'Aspirin', 'M')) #cur.execute("INSERT INTO student(id,name,sex)VALUES(%s,%s,%s)", (2, 'Taxol', 'F')) #self.cur.execute("INSERT INTO conditions(time, location, temperature, humidity) VALUES (NOW(), 'office', 70.0, 50.0);") tmpTime = self.beginTime + datetime.timedelta(microseconds=dictInput['soa']) self.cur.execute("INSERT INTO AllPulses(id, rd, paw, soa, inp, moa, pan) VALUES (%s, %s, %s, make_timestamptz(%s,%s,%s,%s,%s,%s,'Asia/Shanghai'), %s, %s, %s);", (dictInput['id'], dictInput['rd'],dictInput['paw'], tmpTime.year, tmpTime.month, tmpTime.day, tmpTime.hour, tmpTime.minute, tmpTime.second + tmpTime.microsecond*0.000001, dictInput['inp'], dictInput['moa'], dictInput['pan'])) #self.cur.execute("INSERT INTO AllPulses(id, rf, pw, toa, inpulse, doa, pa) VALUES (1, 1000, 20, make_timestamptz(2019,1,2,13,50,1.5684,'Asia/Shanghai'),1, 345, 23);") self.conn.commit() def GetDatas(self): # 获取结果 self.cur.execute('SELECT * FROM conditions ORDER BY time DESC LIMIT 100;') results = self.cur.fetchall() print(results) def Close(self): # 关闭连接 self.conn.commit() self.cur.close() self.conn.close() dictPulse = {} dictPulse['rd'] = 1000000 dictPulse['paw'] = 20 dictPulse['inp'] = 1 dictPulse['moa'] = 35000 dictPulse['pan'] = 14 dbVisitor = TimeScaleDBVisiter() for i in range(100000): dictPulse['id'] = i + 1 dictPulse['soa'] = i * 1000 dbVisitor.InsertPulse(dictPulse) if i % 1000 == 0: print("已插入%d条数据" % (i,))
- 点赞 1
- 收藏
- 分享
- 文章举报
相关文章推荐
- ubuntu16.04安装TimescaleDB
- Docker实现TimescaleDB 基于异步流的主从复制(四)
- Docker实现keepalived + TimescaleDB 的高可用(三)
- 时序数据库DolphinDB和TimescaleDB 性能对比测试报告
- Python3基础 getatime getctime getmtime 获取一个文件的最近访问时间 创建时间 最新修改时间
- Python-time: 时间的访问和转换
- Python3基础 用索引值访问time方法返回来的元组中的年月日时分秒
- TimescaleDB 学习目录
- Python中time模块详解(转)
- EF的DBFirst方式访问SQLite
- examples - visualizing time series data and geographic data using python
- python爬取拉勾网公司信息(您操作太频繁,请稍后访问)
- Python datetime 模块之timedelta
- python3.6.1 time模块记录
- Python时间处理之time模块
- python标准库学习4-time
- python的time模块和datetime模块实例解析
- Python的time模块中的常用方法整理
- python time module
- suds 在python3.x上的安装并访问webservice