TDengine/docs/examples/python/connect_websocket_examples.py

86 lines
2.4 KiB
Python
Raw Normal View History

2024-08-02 07:14:08 +00:00
# ANCHOR: connect
import taosws
2024-08-01 13:07:55 +00:00
def create_connection():
conn = None
2024-08-03 13:58:17 +00:00
host = "localhost"
port = 6041
2024-08-01 13:07:55 +00:00
try:
conn = taosws.connect(
user="root",
password="taosdata",
2024-08-03 13:58:17 +00:00
host=host,
port=port,
2024-08-01 13:07:55 +00:00
)
2024-08-03 13:58:17 +00:00
print(f"Connected to {host}:{port} successfully.");
2024-08-01 13:07:55 +00:00
except Exception as err:
2024-08-14 08:25:22 +00:00
print(f"Failed to connect to {host}:{port} , ErrMessage:{err}")
2024-09-26 03:13:32 +00:00
raise err
2024-08-01 13:07:55 +00:00
return conn
2024-08-03 13:58:17 +00:00
# ANCHOR_END: connect
2024-08-01 13:07:55 +00:00
# ANCHOR: connect_sqlalchemy
def create_connection_with_sqlalchemy():
from sqlalchemy import create_engine
engine = create_engine("taosws://root:taosdata@?hosts=localhost:6041,127.0.0.1:6041")
conn = engine.connect()
return conn
# ANCHOR_END: connect_sqlalchemy
2024-08-01 13:07:55 +00:00
def create_db_table(conn):
2024-08-03 13:58:17 +00:00
# ANCHOR: create_db
2024-08-01 13:07:55 +00:00
try:
conn.execute("CREATE DATABASE IF NOT EXISTS power")
conn.execute("USE power")
2024-08-03 13:58:17 +00:00
conn.execute("CREATE STABLE IF NOT EXISTS meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (groupId INT, location BINARY(24))")
2024-08-01 13:30:34 +00:00
conn.execute("CREATE TABLE IF NOT EXISTS `d0` USING `meters` (groupId, location) TAGS(0, 'Los Angles')")
2024-08-01 13:07:55 +00:00
except Exception as err:
print(f'Exception {err}')
2024-09-26 03:13:32 +00:00
raise err
2024-08-01 13:07:55 +00:00
# ANCHOR_END: create_db
2024-08-01 13:30:34 +00:00
def insert(conn):
2024-08-03 13:58:17 +00:00
# ANCHOR: insert
2024-08-01 13:07:55 +00:00
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)
2024-09-26 05:43:48 +00:00
assert inserted == 4
2024-08-01 13:07:55 +00:00
except Exception as err:
2024-08-03 13:58:17 +00:00
print(f'Exception111 {err}')
2024-09-26 03:13:32 +00:00
raise err
2024-08-01 13:30:34 +00:00
# ANCHOR_END: insert
2024-08-01 13:07:55 +00:00
def query(conn):
2024-08-03 13:58:17 +00:00
# ANCHOR: query
2024-08-01 13:30:34 +00:00
try:
result = conn.query("select * from meters")
num_of_fields = result.field_count
2024-08-03 13:58:17 +00:00
print(num_of_fields)
2024-08-01 13:30:34 +00:00
for row in result:
print(row)
except Exception as err:
2024-08-03 13:58:17 +00:00
print(f'Exception {err}')
2024-09-26 03:13:32 +00:00
raise err
2024-08-01 13:30:34 +00:00
# ANCHOR_END: query
2024-08-01 13:30:34 +00:00
if __name__ == "__main__":
conn = create_connection()
create_db_table(conn)
insert(conn)
if conn:
conn.close()
conn = create_connection_with_sqlalchemy()
2024-08-01 13:30:34 +00:00
query(conn)
if conn:
conn.close()