From 3ed7697218b664a8f674e26662dffe8e86b4cca1 Mon Sep 17 00:00:00 2001 From: menshibin Date: Thu, 1 Aug 2024 21:07:55 +0800 Subject: [PATCH] modify python api --- docs/examples/python/connect_example.py | 25 +++++--- .../python/connect_websocket_examples.py | 61 +++++++++++++------ docs/zh/08-develop/02-sql.md | 2 + 3 files changed, 62 insertions(+), 26 deletions(-) diff --git a/docs/examples/python/connect_example.py b/docs/examples/python/connect_example.py index c31e1cac51..493cd512ee 100644 --- a/docs/examples/python/connect_example.py +++ b/docs/examples/python/connect_example.py @@ -1,14 +1,21 @@ import taos -def test_connection(): +def create_connection(): # all parameters are optional. - conn = taos.connect(host="localhost", - port=6030, - user="root", - password="taosdata") - print('client info:', conn.client_info) - print('server info:', conn.server_info) - conn.close() + conn = None + try: + conn = taosws.connect( + user="root", + password="taosdata", + host="192.168.1.98", + port=6041, + ) + except Exception as err: + print(f'Exception {err}') + finally: + if conn: + conn.close() + if __name__ == "__main__": - test_connection() + create_connection() diff --git a/docs/examples/python/connect_websocket_examples.py b/docs/examples/python/connect_websocket_examples.py index 75e7422a90..1446bdf120 100644 --- a/docs/examples/python/connect_websocket_examples.py +++ b/docs/examples/python/connect_websocket_examples.py @@ -1,26 +1,53 @@ -# ANCHOR: connect import taosws -conn = taosws.connect("taosws://root:taosdata@localhost:6041") +def create_connection(): + conn = None +# ANCHOR: connect + try: + conn = taosws.connect( + user="root", + password="taosdata", + host="localhost", + port=6041, + ) + except Exception as err: + print(f'Exception {err}') # ANCHOR_END: connect + return conn -# ANCHOR: basic -conn.execute("drop database if exists connwspy") -conn.execute("create database if not exists connwspy wal_retention_period 3600 keep 36500 ") -conn.execute("use connwspy") -conn.execute("create table if not exists stb (ts timestamp, c1 int) tags (t1 int)") -conn.execute("create table if not exists tb1 using stb tags (1)") -conn.execute("insert into tb1 values (now, 1)") -conn.execute("insert into tb1 values (now+1s, 2)") -conn.execute("insert into tb1 values (now+2s, 3)") +def create_db_table(conn): +# ANCHOR: create_db + try: + 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')") + except Exception as err: + print(f'Exception {err}') +# ANCHOR_END: create_db -r = conn.execute("select * from stb") -result = conn.query("select * from stb") -num_of_fields = result.field_count -print(num_of_fields) +def insert(conn): + sql = """ + INSERT INTO + power.d1001 USING power.meters TAGS('California.SanFrancisco', 2) + VALUES (NOW + 1a, 10.30000, 219, 0.31000) + (NOW + 2a, 12.60000, 218, 0.33000) (NOW + 3a, 12.30000, 221, 0.31000) + power.d1002 USING power.meters TAGS('California.SanFrancisco', 3) + VALUES (NOW + 1a, 10.30000, 218, 0.25000) + """ + try: + inserted = conn.execute(sql) + assert inserted == 8 + except Exception as err: + print(f'Exception {err}') -for row in result: - print(row) +def query(conn): + result = conn.query("select * from stb") + num_of_fields = result.field_count + print(num_of_fields) + + for row in result: + print(row) # output: # 3 diff --git a/docs/zh/08-develop/02-sql.md b/docs/zh/08-develop/02-sql.md index 7511dde2a2..787a3378ad 100644 --- a/docs/zh/08-develop/02-sql.md +++ b/docs/zh/08-develop/02-sql.md @@ -25,6 +25,8 @@ TDengine 对 SQL 语言提供了全面的支持,允许用户以熟悉的 SQL +- Websocket 连接 +