diff --git a/docs/examples/python/connect_example.py b/docs/examples/python/connect_example.py index 5abc767e85..72d17020c9 100644 --- a/docs/examples/python/connect_example.py +++ b/docs/examples/python/connect_example.py @@ -3,15 +3,18 @@ import taos def create_connection(): # all parameters are optional. conn = None + host = "localhost" + port = 6030 try: conn = taos.connect( user="root", password="taosdata", - host="localhost", - port=6030, + host=host, + port=port, ) + print(f"Connected to {host}:{port} successfully."); except Exception as err: - print(err) + print(f"Failed to connect to {host}:{port} ; Err:{err}") finally: if conn: conn.close() diff --git a/docs/examples/python/connect_rest_example.py b/docs/examples/python/connect_rest_example.py index 266edfafe5..a9e5a00970 100644 --- a/docs/examples/python/connect_rest_example.py +++ b/docs/examples/python/connect_rest_example.py @@ -3,16 +3,16 @@ import taosrest def create_connection(): conn = None + url="http://localhost:6041" try: - conn = taosrest.connect(url="http://localhost:6041", + conn = taosrest.connect(url=url, user="root", password="taosdata", timeout=30) - print("Connection established") - + print(f"Connected to {url} successfully."); except Exception as err: - print(err) + print(f"Failed to connect to {url} ; Err:{err}") finally: if conn: conn.close() diff --git a/docs/examples/python/connect_websocket_examples.py b/docs/examples/python/connect_websocket_examples.py index 939486de28..0cd418eda7 100644 --- a/docs/examples/python/connect_websocket_examples.py +++ b/docs/examples/python/connect_websocket_examples.py @@ -3,36 +3,35 @@ import taosws def create_connection(): conn = None + host = "localhost" + port = 6041 try: conn = taosws.connect( user="root", password="taosdata", - host="localhost", - port=6041, + host=host, + port=port, ) + print(f"Connected to {host}:{port} successfully."); except Exception as err: - print(err) - + print(f"Failed to connect to {host}:{port} ; Err:{err}") + return conn - -# ANCHOR_END: connect + # ANCHOR_END: connect def create_db_table(conn): - # ANCHOR: create_db +# 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 STABLE IF NOT EXISTS meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))") 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 +# ANCHOR: insert sql = """ INSERT INTO power.d1001 USING power.meters TAGS('California.SanFrancisco', 2) @@ -43,26 +42,22 @@ def insert(conn): """ try: inserted = conn.execute(sql) - assert inserted == 4 + assert inserted == 8 except Exception as err: - print(f'Exception {err}') - - + print(f'Exception111 {err}') # ANCHOR_END: insert def query(conn): - # ANCHOR: query +# ANCHOR: query try: result = conn.query("select * from meters") num_of_fields = result.field_count - print(f"query field conunt is {num_of_fields}") + print(num_of_fields) for row in result: print(row) except Exception as err: - print(f'query Exception {err}') - - + print(f'Exception {err}') # ANCHOR_END: query if __name__ == "__main__": diff --git a/docs/examples/python/create_db_native.py b/docs/examples/python/create_db_native.py index 3404363dc3..46f33dbc6e 100644 --- a/docs/examples/python/create_db_native.py +++ b/docs/examples/python/create_db_native.py @@ -1,11 +1,13 @@ import taos conn = None +host = "localhost" +port = 6030 try: - conn = taos.connect(host="localhost", + conn = taos.connect(host=host, + port=port, user="root", - password="taosdata", - port=6030) + password="taosdata") db = "power" # create database @@ -27,7 +29,7 @@ try: assert rowsAffected == 0 except Exception as err: - print(err) + print(f"Failed to create db and table, db addrr:{host}:{port} err:{err}") finally: if conn: conn.close() diff --git a/docs/examples/python/create_db_rest.py b/docs/examples/python/create_db_rest.py index 818fa748ad..a976d86aa0 100644 --- a/docs/examples/python/create_db_rest.py +++ b/docs/examples/python/create_db_rest.py @@ -1,8 +1,9 @@ import taosrest conn = None +url = "http://localhost:6041" try: - conn = taosrest.connect(url="http://localhost:6041", + conn = taosrest.connect(url=url, user="root", password="taosdata", timeout=30) @@ -22,7 +23,7 @@ try: assert rowsAffected == 0 except Exception as err: - print(err) + print(f"Failed to create db and table, url:{url} err:{err}") finally: if conn: conn.close() diff --git a/docs/examples/python/create_db_ws.py b/docs/examples/python/create_db_ws.py index f60411f8e0..edebd8a646 100644 --- a/docs/examples/python/create_db_ws.py +++ b/docs/examples/python/create_db_ws.py @@ -1,76 +1,35 @@ import taosws -db = "power" +conn = None +host = "localhost" +port = 6041 +try: + conn = taosws.connect(user="root", + password="taosdata", + host=host, + port=port) -def prepare(): - conn = None + db = "power" + # create database + rowsAffected = conn.execute(f"CREATE DATABASE IF NOT EXISTS {db}") + assert rowsAffected == 0 - try: - conn = taosws.connect(user="root", - password="taosdata", - host="192.168.1.98", - port=6041) + # change database. + rowsAffected = conn.execute(f"USE {db}") + assert rowsAffected == 0 + + # create super table + rowsAffected = conn.execute( + "CREATE TABLE IF NOT EXISTS `meters` (`ts` TIMESTAMP, `current` FLOAT, `voltage` INT, `phase` FLOAT) TAGS (`groupid` INT, `location` BINARY(16))" + ) + assert rowsAffected == 0 + # create table + rowsAffected = conn.execute("CREATE TABLE IF NOT EXISTS `d0` USING `meters` (groupid, location) TAGS(0, 'Los Angles')") + assert rowsAffected == 0 - # create database - conn.execute(f"drop database if exists {db}") - conn.execute(f"create database {db}") - except Exception as err: - print(err) - finally: - if conn: - conn.close() - -def schemaless_insert(): - conn = None - - lineDemo = [ - "meters,groupid=2,location=California.SanFrancisco current=10.3000002f64,voltage=219i32,phase=0.31f64 1626006833639" - ] - - telnetDemo = ["metric_telnet 1707095283260 4 host=host0 interface=eth0"] - - jsonDemo = [ - '{"metric": "metric_json","timestamp": 1626846400,"value": 10.3, "tags": {"groupid": 2, "location": "California.SanFrancisco", "id": "d1001"}}' - ] - - try: - - conn = taosws.connect(user="root", - password="taosdata", - host="192.168.1.98", - port=6041, - database=db) - - conn.schemaless_insert( - lines=lineDemo, - protocol=taosws.PySchemalessProtocol.Line, - precision=taosws.PySchemalessPrecision.Millisecond, - ttl=1, - req_id=1, - ) - - conn.schemaless_insert( - lines=telnetDemo, - protocol=taosws.PySchemalessProtocol.Telnet, - precision=taosws.PySchemalessPrecision.Microsecond, - ttl=1, - req_id=2, - ) - - conn.schemaless_insert( - lines=jsonDemo, - protocol=taosws.PySchemalessProtocol.Json, - precision=taosws.PySchemalessPrecision.Millisecond, - ttl=1, - req_id=3, - ) - except Exception as err: - print(err) - finally: - if conn: - conn.close() - -if __name__ == '__main__': - prepare() - schemaless_insert() \ No newline at end of file +except Exception as err: + print(f"Failed to create db and table, db addrr:{host}:{port} err:{err}") +finally: + if conn: + conn.close() diff --git a/docs/examples/python/query_native.py b/docs/examples/python/query_native.py index 6e361405e3..da271010eb 100644 --- a/docs/examples/python/query_native.py +++ b/docs/examples/python/query_native.py @@ -20,7 +20,7 @@ try: print(row) except Exception as err: - print(err) + print(f"Failed to query data from power.meters, err:{err}") finally: if conn: conn.close() \ No newline at end of file diff --git a/docs/examples/python/query_rest.py b/docs/examples/python/query_rest.py index fc31e9db33..79f756cbf9 100644 --- a/docs/examples/python/query_rest.py +++ b/docs/examples/python/query_rest.py @@ -8,8 +8,8 @@ try: password="taosdata", timeout=30) - result = client.sql(f"SELECT ts, current, location FROM power.meters limit 100") + result = client.sql(f"SELECT ts, current, location FROM power.meters limit 100", 1) print(result) except Exception as err: - print(err) + print(f"Failed to query data from power.meters, err:{err}") diff --git a/docs/examples/python/query_ws.py b/docs/examples/python/query_ws.py index ee0d40167b..d7480b5caa 100644 --- a/docs/examples/python/query_ws.py +++ b/docs/examples/python/query_ws.py @@ -16,7 +16,7 @@ try: print(row) except Exception as err: - print(err) + print(f"Failed to query data from power.meters, err:{err}") finally: if conn: conn.close() diff --git a/docs/examples/python/reqid_native.py b/docs/examples/python/reqid_native.py index 715b1b39bc..dbf4990461 100644 --- a/docs/examples/python/reqid_native.py +++ b/docs/examples/python/reqid_native.py @@ -7,19 +7,7 @@ try: user="root", password="taosdata") - sql = """ - INSERT INTO - power.d1001 USING power.meters (groupid, location) TAGS(2, 'California.SanFrancisco') - 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 (groupid, location) TAGS(3, 'California.SanFrancisco') - VALUES (NOW + 1a, 10.30000, 218, 0.25000) - """ - affectedRows = conn.execute(sql, 1) - print(f"inserted into {affectedRows} rows to power.meters successfully.") - - result = conn.query("SELECT ts, current, location FROM power.meters limit 100", 2) - print(result) + result = conn.query("SELECT ts, current, location FROM power.meters limit 100", 1) # Get fields from result fields = result.fields for field in fields: @@ -31,7 +19,7 @@ try: print(row) except Exception as err: - print(err) + print(f"Failed to execute sql with reqId, err:{err}") finally: if conn: conn.close() \ No newline at end of file diff --git a/docs/examples/python/reqid_rest.py b/docs/examples/python/reqid_rest.py index 10bc08d12d..847b5a0f54 100644 --- a/docs/examples/python/reqid_rest.py +++ b/docs/examples/python/reqid_rest.py @@ -12,4 +12,4 @@ try: print(result) except Exception as err: - print(err) + print(f"Failed to execute sql with reqId, err:{err}") diff --git a/docs/examples/python/reqid_ws.py b/docs/examples/python/reqid_ws.py index 1226ae8ad6..b541d5d9fd 100644 --- a/docs/examples/python/reqid_ws.py +++ b/docs/examples/python/reqid_ws.py @@ -10,26 +10,13 @@ try: port=6041, ) - sql = """ - INSERT INTO - power.d1001 USING power.meters (groupid, location) TAGS(2, 'California.SanFrancisco') - 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 (groupid, location) TAGS(3, 'California.SanFrancisco') - VALUES (NOW + 1a, 10.30000, 218, 0.25000) - """ - affectedRows = conn.execute_with_req_id(sql, req_id=1) - print(f"inserted into {affectedRows} rows to power.meters successfully.") - - result = conn.query_with_req_id("SELECT ts, current, location FROM power.meters limit 100", req_id=2) - num_of_fields = result.field_count - print(num_of_fields) + result = conn.query_with_req_id("SELECT ts, current, location FROM power.meters limit 100", req_id=1) for row in result: print(row) except Exception as err: - print(err) + print(f"Failed to execute sql with reqId, err:{err}") finally: if conn: conn.close() \ No newline at end of file diff --git a/docs/examples/python/schemaless_native.py b/docs/examples/python/schemaless_native.py index 7faa2176af..52a9d0e472 100644 --- a/docs/examples/python/schemaless_native.py +++ b/docs/examples/python/schemaless_native.py @@ -32,7 +32,7 @@ try: jsonDemo, taos.SmlProtocol.JSON_PROTOCOL, taos.SmlPrecision.MILLI_SECONDS ) except Exception as err: - print(err) + print(f"Failed to insert data with schemaless, err:{err}") finally: if conn: conn.close() diff --git a/docs/examples/python/schemaless_ws.py b/docs/examples/python/schemaless_ws.py index 845f1e65fd..effa3e851e 100644 --- a/docs/examples/python/schemaless_ws.py +++ b/docs/examples/python/schemaless_ws.py @@ -1,81 +1,47 @@ import taosws -db = "power" +conn = None -def prepare(): - conn = None +lineDemo = [ + "meters,groupid=2,location=California.SanFrancisco current=10.3000002f64,voltage=219i32,phase=0.31f64 1626006833639" +] - try: - conn = taosws.connect(user="root", - password="taosdata", - host="taosdata", - port=6041) +telnetDemo = ["metric_telnet 1707095283260 4 host=host0 interface=eth0"] +jsonDemo = [ + '{"metric": "metric_json","timestamp": 1626846400,"value": 10.3, "tags": {"groupid": 2, "location": "California.SanFrancisco", "id": "d1001"}}' +] - # create database - conn.execute(f"drop database if exists {db}") - conn.execute(f"create database {db}") - except Exception as err: - print(err) - raise err - finally: - if conn: - conn.close() +try: + conn = taosws.connect(user="root", + password="taosdata", + host="localhost", + port=6041) -def schemaless_insert(): - conn = None + conn.execute("CREATE DATABASE IF NOT EXISTS power") + conn = conn.execute("USE power") - lineDemo = [ - "meters,groupid=2,location=California.SanFrancisco current=10.3000002f64,voltage=219i32,phase=0.31f64 1626006833639" - ] + conn.schemaless_insert( + lines=lineDemo, + protocol=taosws.PySchemalessProtocol.Line, + precision=taosws.PySchemalessPrecision.Millisecond + ) - telnetDemo = ["metric_telnet 1707095283260 4 host=host0 interface=eth0"] + conn.schemaless_insert( + lines=telnetDemo, + protocol=taosws.PySchemalessProtocol.Telnet, + precision=taosws.PySchemalessPrecision.Microsecond + ) - jsonDemo = [ - '{"metric": "metric_json","timestamp": 1626846400,"value": 10.3, "tags": {"groupid": 2, "location": "California.SanFrancisco", "id": "d1001"}}' - ] + conn.schemaless_insert( + lines=jsonDemo, + protocol=taosws.PySchemalessProtocol.Json, + precision=taosws.PySchemalessPrecision.Millisecond + ) +except Exception as err: + print(f"Failed to insert data with schemaless, err:{err}") + +finally: + if conn: + conn.close() - try: - - conn = taosws.connect(user="root", - password="taosdata", - host="192.168.1.98", - port=6041, - database=db) - - conn.schemaless_insert( - lines=lineDemo, - protocol=taosws.PySchemalessProtocol.Line, - precision=taosws.PySchemalessPrecision.Millisecond, - ttl=1, - req_id=1, - ) - - conn.schemaless_insert( - lines=telnetDemo, - protocol=taosws.PySchemalessProtocol.Telnet, - precision=taosws.PySchemalessPrecision.Microsecond, - ttl=1, - req_id=2, - ) - - conn.schemaless_insert( - lines=jsonDemo, - protocol=taosws.PySchemalessProtocol.Json, - precision=taosws.PySchemalessPrecision.Millisecond, - ttl=1, - req_id=3, - ) - except Exception as err: - print(err) - raise err - finally: - if conn: - conn.close() - -if __name__ == '__main__': - try: - prepare() - schemaless_insert() - except Exception as err: - print(err) \ No newline at end of file diff --git a/docs/examples/python/stmt_native.py b/docs/examples/python/stmt_native.py index f7ef1ba110..8484b57e7e 100644 --- a/docs/examples/python/stmt_native.py +++ b/docs/examples/python/stmt_native.py @@ -42,7 +42,7 @@ try: for j in range (numOfRow): timestamps.append(current + i) currents.append(random.random() * 30) - voltages.append(random.randint(100, 300)) + voltages.append(random.random(100, 300)) phases.append(random.random()) params = taos.new_bind_params(4) @@ -52,10 +52,11 @@ try: params[3].float(phases) stmt.bind_param_batch(params) stmt.execute() - print(f"table {tbname} insert ok.") + affected = stmt.affected_rows() + print(f"table {tbname} insert {affected} rows.") except Exception as err: - print(err) + print(f"Failed to insert to table meters using stmt, error: {err}") finally: if stmt: stmt.close() diff --git a/docs/examples/python/stmt_ws.py b/docs/examples/python/stmt_ws.py index 7f4ef75f74..c213be3566 100644 --- a/docs/examples/python/stmt_ws.py +++ b/docs/examples/python/stmt_ws.py @@ -38,10 +38,10 @@ try: currents = [] voltages = [] phases = [] - for j in range(numOfRow): + for j in range (numOfRow): timestamps.append(current + i) currents.append(random.random() * 30) - voltages.append(random.randint(100, 300)) + voltages.append(random.random(100, 300)) phases.append(random.random()) stmt.bind_param( @@ -55,12 +55,12 @@ try: stmt.add_batch() rows = stmt.execute() - print(f"table {tbname} insert ok.") - + print(f"insert {rows} rows.") + except Exception as err: - print(err) + print(f"Failed to insert to table meters using stmt, error: {err}") finally: if stmt: stmt.close() - if conn: + if conn: conn.close() diff --git a/docs/examples/python/tmq_native.py b/docs/examples/python/tmq_native.py index 7770dda90d..4da6e989a5 100644 --- a/docs/examples/python/tmq_native.py +++ b/docs/examples/python/tmq_native.py @@ -1,6 +1,5 @@ import taos - def prepareMeta(): conn = None try: @@ -46,12 +45,9 @@ def prepareMeta(): if conn: conn.close() - # ANCHOR: create_consumer - - +# ANCHOR: create_consumer from taos.tmq import Consumer - def create_consumer(): try: consumer = Consumer(