liubulong 发表于 2019-12-4 09:24:14

python使用InfluxDB时序数据库增删改查

# coding:utf-8
__author__ = 'liuyonghua526'
from influxdb import InfluxDBClient
# 初始化
client = InfluxDBClient('127.0.0.1', 8086, 'root', '', '')
# 显示已存在数据库
databases = client.get_list_database()
print(databases)
# 创建数据库
new_database = 'test1'
client.create_database(new_database)
# 删除数据库
del_database = 'test1'
client.drop_database(del_database)
# 链接数据库
client = InfluxDBClient('127.0.0.1', 8086, 'root', '', 'test1')
# 显示已存在的表
result = client.query('show measurements;')
print("Result: {0}".format(result))
# 创建新表
json_body = [
    {
      "measurement": "students",
      "tags": {"stuid": "123456"},
      #"time": "2017-03-12T22:00:00Z",
      "fields": {"score": 89}
    }
]
client.write_points(json_body)
# 删除表
del_table = 'students'
client.query("drop measurement "+del_table+"")
# 添加数据
json_body = [
    {
      "measurement": "students",
      "tags": {"stuid": "5689489"},
      "time": "2017-10-19T06:39:54.468978469Z",
      "fields": {"score": 99, "class": 3}
    }
]
client.write_points(json_body)
# 查询数据
result = client.query('select * from students;')
print("Result: {0}".format(result))
# 删除数据
del_table = client.query('delete from students;')
# 更新数据(tags 和 timestamp相同时数据会执行覆盖操作,相当于InfluxDB的更新操作)
json_body = [
    {
      "measurement": "students",

      "tags": {"stuid": "5689489"},

      "time": "2017-10-19T06:39:54.468978469Z",

      "fields": {"score": 99, "class": 4}
    }
]
client.write_points(json_body)
调试使用相应功能时,建议注释掉其他功能。

页: [1]
查看完整版本: python使用InfluxDB时序数据库增删改查