modify python api

This commit is contained in:
menshibin 2024-08-01 21:07:55 +08:00 committed by gccgdb1234
parent f82b92ed1b
commit 3ed7697218
3 changed files with 62 additions and 26 deletions

View File

@ -1,14 +1,21 @@
import taos import taos
def test_connection(): def create_connection():
# all parameters are optional. # all parameters are optional.
conn = taos.connect(host="localhost", conn = None
port=6030, try:
user="root", conn = taosws.connect(
password="taosdata") user="root",
print('client info:', conn.client_info) password="taosdata",
print('server info:', conn.server_info) host="192.168.1.98",
conn.close() port=6041,
)
except Exception as err:
print(f'Exception {err}')
finally:
if conn:
conn.close()
if __name__ == "__main__": if __name__ == "__main__":
test_connection() create_connection()

View File

@ -1,26 +1,53 @@
# ANCHOR: connect
import taosws 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 # ANCHOR_END: connect
return conn
# ANCHOR: basic def create_db_table(conn):
conn.execute("drop database if exists connwspy") # ANCHOR: create_db
conn.execute("create database if not exists connwspy wal_retention_period 3600 keep 36500 ") try:
conn.execute("use connwspy") conn.execute("CREATE DATABASE IF NOT EXISTS power")
conn.execute("create table if not exists stb (ts timestamp, c1 int) tags (t1 int)") conn.execute("USE power")
conn.execute("create table if not exists tb1 using stb tags (1)") conn.execute("CREATE STABLE IF NOT EXISTS meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))")
conn.execute("insert into tb1 values (now, 1)") conn.execute("CREATE TABLE `d0` USING `meters` TAGS(0, 'Los Angles')")
conn.execute("insert into tb1 values (now+1s, 2)") except Exception as err:
conn.execute("insert into tb1 values (now+2s, 3)") print(f'Exception {err}')
# ANCHOR_END: create_db
r = conn.execute("select * from stb") def insert(conn):
result = conn.query("select * from stb") sql = """
num_of_fields = result.field_count INSERT INTO
print(num_of_fields) 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: def query(conn):
print(row) result = conn.query("select * from stb")
num_of_fields = result.field_count
print(num_of_fields)
for row in result:
print(row)
# output: # output:
# 3 # 3

View File

@ -25,6 +25,8 @@ TDengine 对 SQL 语言提供了全面的支持,允许用户以熟悉的 SQL
</TabItem> </TabItem>
<TabItem label="Python" value="python"> <TabItem label="Python" value="python">
- Websocket 连接
</TabItem> </TabItem>
<TabItem label="Go" value="go"> <TabItem label="Go" value="go">
</TabItem> </TabItem>