From 7c13635487514b2f660c25c471fbdae7b1cbd466 Mon Sep 17 00:00:00 2001 From: menshibin Date: Thu, 1 Aug 2024 21:30:34 +0800 Subject: [PATCH] modify python api --- .../python/connect_websocket_examples.py | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/docs/examples/python/connect_websocket_examples.py b/docs/examples/python/connect_websocket_examples.py index 1446bdf120..69c3900187 100644 --- a/docs/examples/python/connect_websocket_examples.py +++ b/docs/examples/python/connect_websocket_examples.py @@ -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): +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") - num_of_fields = result.field_count - print(num_of_fields) +# 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) + 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()