modify python api

This commit is contained in:
menshibin 2024-08-01 21:30:34 +08:00 committed by gccgdb1234
parent a10bbd4cdf
commit 7c13635487
1 changed files with 23 additions and 14 deletions

View File

@ -7,7 +7,7 @@ def create_connection():
conn = taosws.connect(
user="root",
password="taosdata",
host="localhost",
host="192.168.1.98",
port=6041,
)
except Exception as err:
@ -21,12 +21,13 @@ def create_db_table(conn):
conn.execute("CREATE DATABASE IF NOT EXISTS power")
conn.execute("USE power")
conn.execute("CREATE STABLE IF NOT EXISTS meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))")
conn.execute("CREATE TABLE `d0` USING `meters` TAGS(0, 'Los Angles')")
conn.execute("CREATE TABLE IF NOT EXISTS `d0` USING `meters` (groupId, location) TAGS(0, 'Los Angles')")
except Exception as err:
print(f'Exception {err}')
# ANCHOR_END: create_db
def insert(conn):
# ANCHOR: insert
sql = """
INSERT INTO
power.d1001 USING power.meters TAGS('California.SanFrancisco', 2)
@ -39,18 +40,26 @@ def insert(conn):
inserted = conn.execute(sql)
assert inserted == 8
except Exception as err:
print(f'Exception {err}')
print(f'Exception111 {err}')
# ANCHOR_END: insert
def query(conn):
result = conn.query("select * from stb")
# ANCHOR: query
try:
result = conn.query("select * from meters")
num_of_fields = result.field_count
print(num_of_fields)
for row in result:
print(row)
except Exception as err:
print(f'Exception {err}')
# ANCHOR_END: query
# output:
# 3
# ('2023-02-28 15:56:13.329 +08:00', 1, 1)
# ('2023-02-28 15:56:13.333 +08:00', 2, 1)
# ('2023-02-28 15:56:13.337 +08:00', 3, 1)
if __name__ == "__main__":
conn = create_connection()
create_db_table(conn)
insert(conn)
query(conn)
if conn:
conn.close()