From 2c35ec8ae9a8859871cc4330add7872e9f257dcb Mon Sep 17 00:00:00 2001
From: Alex Duan <417921451@qq.com>
Date: Mon, 30 Dec 2024 16:11:11 +0800
Subject: [PATCH] docs: add stmt2_native.py example
---
docs/en/07-develop/05-stmt.md | 2 +-
docs/examples/python/stmt2_native.py | 78 ++++++++++++++++++++++++++++
docs/zh/07-develop/05-stmt.md | 2 +-
tests/docs-examples-test/python.sh | 1 +
4 files changed, 81 insertions(+), 2 deletions(-)
create mode 100644 docs/examples/python/stmt2_native.py
diff --git a/docs/en/07-develop/05-stmt.md b/docs/en/07-develop/05-stmt.md
index 485315bc93..11b055bcf9 100644
--- a/docs/en/07-develop/05-stmt.md
+++ b/docs/en/07-develop/05-stmt.md
@@ -98,7 +98,7 @@ This is a [more detailed parameter binding example](https://github.com/taosdata/
```python
-{{#include docs/examples/python/stmt_native.py}}
+{{#include docs/examples/python/stmt2_native.py}}
```
diff --git a/docs/examples/python/stmt2_native.py b/docs/examples/python/stmt2_native.py
new file mode 100644
index 0000000000..7e6d59466e
--- /dev/null
+++ b/docs/examples/python/stmt2_native.py
@@ -0,0 +1,78 @@
+import taos
+from datetime import datetime
+import random
+
+conn = None
+stmt2 = None
+host="localhost"
+port=6030
+try:
+ # 1 connect
+ conn = taos.connect(
+ user="root",
+ password="taosdata",
+ host=host,
+ port=port,
+ )
+
+ # 2 create db and table
+ conn.execute("CREATE DATABASE IF NOT EXISTS power")
+ conn.execute("USE power")
+ conn.execute(
+ "CREATE TABLE IF NOT EXISTS `meters` (`ts` TIMESTAMP, `current` FLOAT, `voltage` INT, `phase` FLOAT) TAGS (`groupid` INT, `location` BINARY(16))"
+ )
+
+ # 3 prepare
+ sql = "INSERT INTO ? USING meters (groupid, location) TAGS(?,?) VALUES (?,?,?,?)"
+ stmt2 = conn.statement2(sql)
+
+ # table name array
+ tbnames = ["d0","d1","d2"]
+ # tag data array
+ tags = [
+ [1, "BeiJing"],
+ [2, None],
+ [3, "ShangHai"]
+ ]
+ # column data array
+ datas = [
+ # d0 tabled
+ [
+ [1601481600000,1601481600001,1601481600002,1601481600003,1601481600004,1601481600005],
+ [10.1, 10.2, 10.3, 10.4, 10.5 ,None ],
+ [98, None, 60, 100, 99 ,128 ],
+ [0, 1, 0, 0, 1 ,0 ]
+ ],
+ # d1 tabled
+ [
+ [1601481700000,1601481700001,1601481700002,1601481700003,1601481700004,1601481700005],
+ [10.1, 10.2, 10.3, 10.4, 10.5 ,11.2 ],
+ [98, 80, 60, 100, 99 ,128 ],
+ [0, 1, 0, 0, 1 ,0 ]
+ ],
+ # d2 tabled
+ [
+ [1601481800000,1601481800001,1601481800002,1601481800003,1601481800004,1601481800005],
+ [10.1, 10.2, 10.3, 10.4, 10.5 ,13.4 ],
+ [98, 80, 60, 100, 99 ,128 ],
+ [0, 1, 0, None, 1 ,0 ]
+ ],
+ ]
+
+ # 4 bind param
+ stmt2.bind_param(tbnames, tags, datas)
+
+ # 5 execute
+ stmt2.execute()
+
+ # show
+ print(f"Successfully inserted with stmt2 to power.meters.")
+
+except Exception as err:
+ print(f"Failed to insert to table meters using stmt2, ErrMessage:{err}")
+ raise err
+finally:
+ if stmt2:
+ stmt2.close()
+ if conn:
+ conn.close()
diff --git a/docs/zh/07-develop/05-stmt.md b/docs/zh/07-develop/05-stmt.md
index 045126b333..1917a86e74 100644
--- a/docs/zh/07-develop/05-stmt.md
+++ b/docs/zh/07-develop/05-stmt.md
@@ -93,7 +93,7 @@ import TabItem from "@theme/TabItem";
```python
-{{#include docs/examples/python/stmt_native.py}}
+{{#include docs/examples/python/stmt2_native.py}}
```
diff --git a/tests/docs-examples-test/python.sh b/tests/docs-examples-test/python.sh
index 3a9812637c..536155437b 100644
--- a/tests/docs-examples-test/python.sh
+++ b/tests/docs-examples-test/python.sh
@@ -196,3 +196,4 @@ check_transactions || exit 1
reset_cache || exit 1
python3 tmq_websocket_example.py
+python3 stmt2_native.py
\ No newline at end of file