Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
8d15fc72
T
TDengine
项目概览
taosdata
/
TDengine
大约 2 年 前同步成功
通知
1192
Star
22018
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
8d15fc72
编写于
4月 27, 2022
作者:
B
Bo Ding
提交者:
GitHub
4月 27, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
docs: python connector document part2 (#11941)
* docs: python document part2 * docs: refine some words * docs: typo
上级
8deede11
变更
11
隐藏空白更改
内联
并排
Showing
11 changed file
with
365 addition
and
30 deletion
+365
-30
docs-cn/14-reference/03-connector/python.mdx
docs-cn/14-reference/03-connector/python.mdx
+108
-26
docs-examples/c/connect_example.c
docs-examples/c/connect_example.c
+3
-1
docs-examples/c/error_handle_example.c
docs-examples/c/error_handle_example.c
+24
-0
docs-examples/python/conn_native_pandas.py
docs-examples/python/conn_native_pandas.py
+19
-0
docs-examples/python/conn_rest_pandas.py
docs-examples/python/conn_rest_pandas.py
+19
-0
docs-examples/python/connect_native_reference.py
docs-examples/python/connect_native_reference.py
+12
-3
docs-examples/python/connect_rest_examples.py
docs-examples/python/connect_rest_examples.py
+47
-0
docs-examples/python/connection_usage_native_reference.py
docs-examples/python/connection_usage_native_reference.py
+45
-0
docs-examples/python/cursor_usage_native_reference.py
docs-examples/python/cursor_usage_native_reference.py
+32
-0
docs-examples/python/handle_exception.py
docs-examples/python/handle_exception.py
+23
-0
docs-examples/python/result_set_examples.py
docs-examples/python/result_set_examples.py
+33
-0
未找到文件。
docs-cn/14-reference/03-connector/python.mdx
浏览文件 @
8d15fc72
...
@@ -61,10 +61,10 @@ pip3 uninstall taos taospy
...
@@ -61,10 +61,10 @@ pip3 uninstall taos taospy
pip3 install taospy
pip3 install taospy
```
```
也可
用
指定某个特定版本安装。
也可
以
指定某个特定版本安装。
```
```
pip3 install taospy==2.
2.5
pip3 install taospy==2.
3.0
```
```
</TabItem>
</TabItem>
...
@@ -82,7 +82,7 @@ pip3 install git+https://github.com/taosdata/taos-connector-python.git
...
@@ -82,7 +82,7 @@ pip3 install git+https://github.com/taosdata/taos-connector-python.git
<Tabs groupId="connect" default="native">
<Tabs groupId="connect" default="native">
<TabItem value="native" label="原生连接">
<TabItem value="native" label="原生连接">
对于
本地连接,需要验证客户端驱动和 Python 连接器本身是否正确安装。如果导入 `taos` 模块成功,在
说明已经正确安装了客户端驱动和 Python 连接器。可在 Python 交互式 Shell 中输入:
对于
原生连接,需要验证客户端驱动和 Python 连接器本身是否都正确安装。如果能成功导入 `taos` 模块,则
说明已经正确安装了客户端驱动和 Python 连接器。可在 Python 交互式 Shell 中输入:
```python
```python
import taos
import taos
...
@@ -91,7 +91,7 @@ import taos
...
@@ -91,7 +91,7 @@ import taos
</TabItem>
</TabItem>
<TabItem value="rest" label="REST 连接">
<TabItem value="rest" label="REST 连接">
对于 REST 连接,只需验证
`taosrest` 模块是否能成功导入
。可在 Python 交互式 Shell 中输入:
对于 REST 连接,只需验证
是否能成功导入 `taosrest` 模块
。可在 Python 交互式 Shell 中输入:
```python
```python
import taosrest
import taosrest
...
@@ -101,12 +101,12 @@ import taosrest
...
@@ -101,12 +101,12 @@ import taosrest
</Tabs>
</Tabs>
:::tip
:::tip
如果系统上有多个版本的 Python,则可能有多个 `pip` 命令。要确保使用的 `pip` 命令路径是正确的。上面我们用 `pip3` 命令安装,排除了使用 Python 2.x 版本对应的 `pip` 的可能性。但是如果系统上有多个 Python 3.x 版本,仍需
注意验证。最简单的验证方式是, 查看 `taospy` 最终的安装位置是否正确。在命令再次输入 `pip3 isn
tall taospy`, 就会打印出 `taospy` 的具体安装位置,比如在 Widnows 上:
如果系统上有多个版本的 Python,则可能有多个 `pip` 命令。要确保使用的 `pip` 命令路径是正确的。上面我们用 `pip3` 命令安装,排除了使用 Python 2.x 版本对应的 `pip` 的可能性。但是如果系统上有多个 Python 3.x 版本,仍需
检查安装路径是否正确。最简单的验证方式是,在命令再次输入 `pip3 ins
tall taospy`, 就会打印出 `taospy` 的具体安装位置,比如在 Widnows 上:
```
```
C:\> pip3 install taospy
C:\> pip3 install taospy
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: taospy in c:\users\username\appdata\local\programs\python\python310\lib\site-packages (2.
2.5
)
Requirement already satisfied: taospy in c:\users\username\appdata\local\programs\python\python310\lib\site-packages (2.
3.0
)
```
```
:::
:::
...
@@ -115,7 +115,7 @@ Requirement already satisfied: taospy in c:\users\username\appdata\local\program
...
@@ -115,7 +115,7 @@ Requirement already satisfied: taospy in c:\users\username\appdata\local\program
### 连通性测试
### 连通性测试
在用连接器建立连接之前,建议先
做本地到 TDengine 集群的连通性测试
。
在用连接器建立连接之前,建议先
测试本地 TDengine CLI 到 TDengine 集群的连
。
<Tabs>
<Tabs>
<TabItem value="native" label="原生连接">
<TabItem value="native" label="原生连接">
...
@@ -170,20 +170,17 @@ curl -u root:taosdata http://<FQDN>:<PORT>/rest/sql -d "select server_version()"
...
@@ -170,20 +170,17 @@ curl -u root:taosdata http://<FQDN>:<PORT>/rest/sql -d "select server_version()"
{{#include docs-examples/python/connect_native_reference.py}}
{{#include docs-examples/python/connect_native_reference.py}}
```
```
`connect` 函数的所有参数都是可选
参数。下表
是连接参数的具体说明:
`connect` 函数的所有参数都是可选
的关键字参数。下面
是连接参数的具体说明:
| 参数 | 含义 | 默认值 | 作用范围 |
- `host` : 要连接的节点的 FQDN。 没有默认值。如果不同提供此参数,则会连接客户端配置文件中的 firstEP。
| -------- | -------------------------------------------------------------------------------------------------- | ---------------------------------------------------------- | -------- |
- `user` :TDengine 用户名。 默认值是 root。
| host | 要连接的节点的 FQDN <br/> 如果不同提供此参数,则会连接客户端配置文件中的 firstEP | 无 | 本次连接 |
- `password` : TDengine 用户密码。 默认值是 taosdata。
| user | 用户名 | root | 本次连接 |
- `port` : 要连接的数据节点的起始端口,即 serverPort 配置。默认值是 6030。只有在提供了 host 参数的时候,这个参数才生效。
| passowrd | 密码 | taosdata | 本次连接 |
- `config` : 客户端配置文件路径。 在 Windows 系统上默认是 `C:\TDengine\cfg`。 在 Linux 系统上默认是 `/etc/taos/`。
| database | 默认使用的数据库,<br/> 如果有值那么这个数据库必须存在 | 无 | 本次连接 |
- `timezone` : 查询结果中 TIMESTAMP 类型的数据,转换为 python 的 datetime 对象时使用的时区。默认为本地时区。
| port | 要连接的数据节点的起始端口,即 serverPort 配置。<br/> 只有在提供了 host 参数的时候,这个参数才生效 | 6030 | 本次连接 |
| config | 客户端配置文件路径 | 1. Windows: `C:\TDengine\cfg` <br/> 2. Linux: `/etc/taos/` | 本进程 |
| timezone | 客户端时区 | 客户端配置文件中 timezone | 本进程 |
:::warning
:::warning
当参数的作为范围是进程级别时,只有建立第一个连接时提供的参数生效。也就是说: 同一个进程的两个连接,必须使用相同的客户端配置。但这并不妨碍同一进程连接两个不同的集群,因为 host 参数是连接级别的
。
`config` 和 `timezone` 都是进程级别的配置。建议一个进程建立的所有连接都使用相同的参数值。否则可能产生无法预知的错误
。
:::
:::
:::tip
:::tip
...
@@ -194,15 +191,97 @@ curl -u root:taosdata http://<FQDN>:<PORT>/rest/sql -d "select server_version()"
...
@@ -194,15 +191,97 @@ curl -u root:taosdata http://<FQDN>:<PORT>/rest/sql -d "select server_version()"
</TabItem>
</TabItem>
<TabItem value="rest" label="REST 连接">
<TabItem value="rest" label="REST 连接">
```python
{{#include docs-examples/python/connect_rest_examples.py:connect}}
```
`connect` 函数的所有参数都是可选的关键字参数。下面是连接参数的具体说明:
- `host`: 要连接的主机。默认是 localhost。
- `user`: TDenigne 用户名。默认是 root。
- `password`: TDeingine 用户密码。默认是 taosdata。
- `port`: taosAdapter REST 服务监听端口。默认是 6041.
- `timeout`: HTTP 请求超时时间。单位为秒。默认为 `socket._GLOBAL_DEFAULT_TIMEOUT`。 一般无需配置。
:::note
:::
</TabItem>
</TabItem>
</Tabs>
</Tabs>
##
开发指南
##
示例程序
### 基本使用
### 基本使用
<Tabs default="native" groupId="connect">
<TabItem value="native" label="原生连接">
#### TaosConnection 类的使用
`TaosConnection` 类既包含对 PEP249 Connection 接口的实现(如:`cursor`方法和 `close` 方法),也包含很多扩展功能(如: `execute`、 `query`、`schemaless_insert` 和 `subscribe` 方法。
```python title="execute 方法"
{{#include docs-examples/python/connection_usage_native_reference.py:insert}}
```
```python title="query 方法"
{{#include docs-examples/python/connection_usage_native_reference.py:query}}
```
:::tip
查询结果只能获取一次。比如上面的示例中 `featch_all` 和 `fetch_all_into_dict` 只能用一个。重复获取得到的结果为空列表。
:::
#### TaosResult 类的使用
上面 `TaosConnection` 类的使用示例中,我们已经展示了两种获取查询结果的方法: `featch_all` 和 `fetch_all_into_dict`。除此之外 `TaosResult` 还提供了按行迭代(`rows_iter`)或按数据块迭代(`blocks_iter`)结果集的方法。在查询数据量较大的场景,使用这两个方法会更高效。
```python title="blocks_iter 方法"
{{#include docs-examples/python/result_set_examples.py}}
```
#### TaosCursor 类的使用
`TaosConnection` 类和 `TaosResult` 类已经实现了原生接口的所有功能。如果你对 PEP249 规范中的接口比较熟悉也可以使用 `TaosCursor` 类提供的方法。
```python title="TaosCursor 的使用"
{{#include docs-examples/python/cursor_usage_native_reference.py}}
```
:::note
TaosCursor 类使用原生连接进行写入、查询操作。在客户端多线程的场景下,这个游标实例必须保持线程独享,不能跨线程共享使用,否则会导致返回结果出现错误。
:::
</TabItem>
<TabItem value="rest" label="REST 连接">
```python
{{#include docs-examples/python/connect_rest_examples.py:basic}}
```
</TabItem>
</Tabs>
### 与 pandas 一起使用
### 与 pandas 一起使用
<Tabs default="native" groupId="connect">
<TabItem value="native" label="原生连接">
```python
{{#include docs-examples/python/conn_native_pandas.py}}
```
</TabItem>
<TabItem value="rest" label="REST 连接">
```python
{{#include docs-examples/python/conn_rest_pandas.py}}
```
</TabItem>
</Tabs>
### 其它示例程序
### 其它示例程序
| 示例程序链接 | 示例程序内容 |
| 示例程序链接 | 示例程序内容 |
...
@@ -216,23 +295,26 @@ curl -u root:taosdata http://<FQDN>:<PORT>/rest/sql -d "select server_version()"
...
@@ -216,23 +295,26 @@ curl -u root:taosdata http://<FQDN>:<PORT>/rest/sql -d "select server_version()"
## 其它说明
## 其它说明
###
关于纳秒 (nanosecond) 在 Python 连接器中的说明
###
异常处理
由于目前 Python 对 nanosecond 支持的不完善(参见链接 1. 2. ),目前的实现方式是在 nanosecond 精度时返回整数,而不是 ms 和 us 返回的 datetime 类型,应用开发者需要自行处理,建议使用 pandas 的 to_datetime()。未来如果 Python 正式完整支持了纳秒,涛思数据可能会修改相关接口。
所有数据库操作如果出现异常,都会直接抛出来。由应用程序负责异常处理。比如:
```python
{{#include docs-examples/python/handle_exception.py}}
```
### 关于纳秒 (nanosecond)
由于目前 Python 对 nanosecond 支持的不完善(参见下面的链接),目前的实现方式是在 nanosecond 精度时返回整数,而不是 ms 和 us 返回的 datetime 类型,应用开发者需要自行处理,建议使用 pandas 的 to_datetime()。未来如果 Python 正式完整支持了纳秒,Python 连接器可能会修改相关接口。
1. https://stackoverflow.com/questions/10611328/parsing-datetime-strings-containing-nanoseconds
1. https://stackoverflow.com/questions/10611328/parsing-datetime-strings-containing-nanoseconds
2. https://www.python.org/dev/peps/pep-0564/
2. https://www.python.org/dev/peps/pep-0564/
### 关于 TaosCursor 类的说明
这个类对应原生连接进行的写入、查询操作。在客户端多线程的场景下,这个游标实例必须保持线程独享,不能跨线程共享使用,否则会导致返回结果出现错误。
## 常见问题
## 常见问题
欢迎[提问或报告问题](https://github.com/taosdata/taos-connector-python/issues)。
欢迎[提问或报告问题](https://github.com/taosdata/taos-connector-python/issues)。
## 重要更新
## 重要更新
| 连接器版本 | 主要更新 | 发布日期 |
| 连接器版本 | 主要更新 | 发布日期 |
...
...
docs-examples/c/connect_example.c
浏览文件 @
8d15fc72
...
@@ -13,7 +13,9 @@ int main() {
...
@@ -13,7 +13,9 @@ int main() {
uint16_t
port
=
0
;
// 0 means use the default port
uint16_t
port
=
0
;
// 0 means use the default port
TAOS
*
taos
=
taos_connect
(
host
,
user
,
passwd
,
db
,
port
);
TAOS
*
taos
=
taos_connect
(
host
,
user
,
passwd
,
db
,
port
);
if
(
taos
==
NULL
)
{
if
(
taos
==
NULL
)
{
printf
(
"failed to connect to server
\n
"
);
int
errono
=
taos_errno
(
NULL
);
char
*
msg
=
taos_errstr
(
NULL
);
printf
(
"%d, %s
\n
"
,
errono
,
msg
);
}
else
{
}
else
{
printf
(
"connected
\n
"
);
printf
(
"connected
\n
"
);
taos_close
(
taos
);
taos_close
(
taos
);
...
...
docs-examples/c/error_handle_example.c
0 → 100644
浏览文件 @
8d15fc72
// compile with
// gcc error_handle_example.c -o error_handle_example -ltaos
#include <stdio.h>
#include <stdlib.h>
#include "taos.h"
int
main
()
{
const
char
*
host
=
"localhost"
;
const
char
*
user
=
"root"
;
const
char
*
passwd
=
"taosdata"
;
// if don't want to connect to a default db, set it to NULL or ""
const
char
*
db
=
"notexist"
;
uint16_t
port
=
0
;
// 0 means use the default port
TAOS
*
taos
=
taos_connect
(
host
,
user
,
passwd
,
db
,
port
);
if
(
taos
==
NULL
)
{
int
errono
=
taos_errno
(
NULL
);
char
*
msg
=
taos_errstr
(
NULL
);
printf
(
"%d, %s
\n
"
,
errono
,
msg
);
}
else
{
printf
(
"connected
\n
"
);
taos_close
(
taos
);
}
taos_cleanup
();
}
docs-examples/python/conn_native_pandas.py
0 → 100644
浏览文件 @
8d15fc72
import
pandas
from
sqlalchemy
import
create_engine
engine
=
create_engine
(
"taos://root:taosdata@localhost:6030/power"
)
df
=
pandas
.
read_sql
(
"SELECT * FROM meters"
,
engine
)
# print index
print
(
df
.
index
)
# print data type of element in ts column
print
(
type
(
df
.
ts
[
0
]))
print
(
df
.
head
(
3
))
# output:
# RangeIndex(start=0, stop=8, step=1)
# <class 'pandas._libs.tslibs.timestamps.Timestamp'>
# ts current voltage phase location groupid
# 0 2018-10-03 14:38:05.000 10.3 219 0.31 beijing.chaoyang 2
# 1 2018-10-03 14:38:15.000 12.6 218 0.33 beijing.chaoyang 2
# 2 2018-10-03 14:38:16.800 12.3 221 0.31 beijing.chaoyang 2
docs-examples/python/conn_rest_pandas.py
0 → 100644
浏览文件 @
8d15fc72
import
pandas
from
sqlalchemy
import
create_engine
engine
=
create_engine
(
"taosrest://root:taosdata@localhost:6041"
)
df
:
pandas
.
DataFrame
=
pandas
.
read_sql
(
"SELECT * FROM power.meters"
,
engine
)
# print index
print
(
df
.
index
)
# print data type of element in ts column
print
(
type
(
df
.
ts
[
0
]))
print
(
df
.
head
(
3
))
# output:
# <class 'datetime.datetime'>
# RangeIndex(start=0, stop=8, step=1)
# ts current ... location groupid
# 0 2018-10-03 14:38:05+08:00 10.3 ... beijing.chaoyang 2
# 1 2018-10-03 14:38:15+08:00 12.6 ... beijing.chaoyang 2
# 2 2018-10-03 14:38:16.800000+08:00 12.3 ... beijing.chaoyang 2
docs-examples/python/connect_native_reference.py
浏览文件 @
8d15fc72
...
@@ -5,7 +5,16 @@ conn: taos.TaosConnection = taos.connect(host="localhost",
...
@@ -5,7 +5,16 @@ conn: taos.TaosConnection = taos.connect(host="localhost",
password
=
"taosdata"
,
password
=
"taosdata"
,
database
=
"test"
,
database
=
"test"
,
port
=
6030
,
port
=
6030
,
config
=
"C:\TDengine\cfg"
,
# for linux the default value is /etc/taos
config
=
"/etc/taos"
,
# for windows the default value is C:\TDengine\cfg
timezone
=
"Asia/Shanghai"
# default your host's timezone
timezone
=
"Asia/Shanghai"
)
# default your host's timezone
)
server_version
=
conn
.
server_info
print
(
"server_version"
,
server_version
)
client_version
=
conn
.
client_info
print
(
"client_version"
,
client_version
)
# 2.4.0.16
conn
.
close
()
conn
.
close
()
# possible output:
# 2.4.0.16
# 2.4.0.16
docs-examples/python/connect_rest_examples.py
0 → 100644
浏览文件 @
8d15fc72
# ANCHOR: connect
from
taosrest
import
connect
,
TaosRestConnection
,
TaosRestCursor
conn
:
TaosRestConnection
=
connect
(
host
=
"localhost"
,
user
=
"root"
,
password
=
"taosdata"
,
port
=
6041
,
timeout
=
30
)
# ANCHOR_END: connect
# ANCHOR: basic
# create STable
cursor
=
conn
.
cursor
()
cursor
.
execute
(
"DROP DATABASE IF EXISTS power"
)
cursor
.
execute
(
"CREATE DATABASE power"
)
cursor
.
execute
(
"CREATE STABLE power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (location BINARY(64), groupId INT)"
)
# insert data
cursor
.
execute
(
"""INSERT INTO power.d1001 USING power.meters TAGS(Beijing.Chaoyang, 2) VALUES ('2018-10-03 14:38:05.000', 10.30000, 219, 0.31000) ('2018-10-03 14:38:15.000', 12.60000, 218, 0.33000) ('2018-10-03 14:38:16.800', 12.30000, 221, 0.31000)
power.d1002 USING power.meters TAGS(Beijing.Chaoyang, 3) VALUES ('2018-10-03 14:38:16.650', 10.30000, 218, 0.25000)
power.d1003 USING power.meters TAGS(Beijing.Haidian, 2) VALUES ('2018-10-03 14:38:05.500', 11.80000, 221, 0.28000) ('2018-10-03 14:38:16.600', 13.40000, 223, 0.29000)
power.d1004 USING power.meters TAGS(Beijing.Haidian, 3) VALUES ('2018-10-03 14:38:05.000', 10.80000, 223, 0.29000) ('2018-10-03 14:38:06.500', 11.50000, 221, 0.35000)"""
)
print
(
"affected rows"
,
cursor
.
rowcount
)
# query data
cursor
.
execute
(
"SELECT * FROM power.meters"
)
# get column names from cursor
column_names
=
[
meta
[
0
]
for
meta
in
cursor
.
description
]
# get rows
data
:
list
[
tuple
]
=
cursor
.
fetchall
()
print
(
column_names
)
for
row
in
data
:
print
(
row
)
# output:
# affected rows 8
# ['ts', 'current', 'voltage', 'phase', 'location', 'groupid']
# [datetime.datetime(2018, 10, 3, 14, 38, 5, tzinfo=tzinfo(480,'+08:00')), 10.3, 219, 0.31, 'beijing.chaoyang', 2]
# [datetime.datetime(2018, 10, 3, 14, 38, 15, tzinfo=tzinfo(480,'+08:00')), 12.6, 218, 0.33, 'beijing.chaoyang', 2]
# [datetime.datetime(2018, 10, 3, 14, 38, 16, 800000, tzinfo=tzinfo(480,'+08:00')), 12.3, 221, 0.31, 'beijing.chaoyang', 2]
# [datetime.datetime(2018, 10, 3, 14, 38, 16, 650000, tzinfo=tzinfo(480,'+08:00')), 10.3, 218, 0.25, 'beijing.chaoyang', 3]
# [datetime.datetime(2018, 10, 3, 14, 38, 5, 500000, tzinfo=tzinfo(480,'+08:00')), 11.8, 221, 0.28, 'beijing.haidian', 2]
# [datetime.datetime(2018, 10, 3, 14, 38, 16, 600000, tzinfo=tzinfo(480,'+08:00')), 13.4, 223, 0.29, 'beijing.haidian', 2]
# [datetime.datetime(2018, 10, 3, 14, 38, 5, tzinfo=tzinfo(480,'+08:00')), 10.8, 223, 0.29, 'beijing.haidian', 3]
# [datetime.datetime(2018, 10, 3, 14, 38, 6, 500000, tzinfo=tzinfo(480,'+08:00')), 11.5, 221, 0.35, 'beijing.haidian', 3]
# ANCHOR_END: basic
docs-examples/python/connection_usage_native_reference.py
0 → 100644
浏览文件 @
8d15fc72
import
taos
# ANCHOR: insert
conn
=
taos
.
connect
()
# Execute a sql, ignore the result set, just get affected rows. It's useful for DDL and DML statement.
conn
.
execute
(
"DROP DATABASE IF EXISTS test"
)
conn
.
execute
(
"CREATE DATABASE test"
)
# change database. same as execute "USE db"
conn
.
select_db
(
"test"
)
conn
.
execute
(
"CREATE STABLE weather(ts TIMESTAMP, temperature FLOAT) TAGS (location INT)"
)
affected_row
:
int
=
conn
.
execute
(
"INSERT INTO t1 USING weather TAGS(1) VALUES (now, 23.5) (now+1m, 23.5) (now+2m 24.4)"
)
print
(
"affected_row"
,
affected_row
)
# output:
# affected_row 3
# ANCHOR_END: insert
# ANCHOR: query
# Execute a sql and get its result set. It's useful for SELECT statement
result
:
taos
.
TaosResult
=
conn
.
query
(
"SELECT * from weather"
)
# Get fields from result
fields
:
taos
.
field
.
TaosFields
=
result
.
fields
for
field
in
fields
:
print
(
field
)
# {name: ts, type: 9, bytes: 8}
# output:
# {name: ts, type: 9, bytes: 8}
# {name: temperature, type: 6, bytes: 4}
# {name: location, type: 4, bytes: 4}
# Get data from result as list of tuple
data
=
result
.
fetch_all
()
print
(
data
)
# output:
# [(datetime.datetime(2022, 4, 27, 9, 4, 25, 367000), 23.5, 1), (datetime.datetime(2022, 4, 27, 9, 5, 25, 367000), 23.5, 1), (datetime.datetime(2022, 4, 27, 9, 6, 25, 367000), 24.399999618530273, 1)]
# Or get data from result as a list of dict
# map_data = result.fetch_all_into_dict()
# print(map_data)
# output:
# [{'ts': datetime.datetime(2022, 4, 27, 9, 1, 15, 343000), 'temperature': 23.5, 'location': 1}, {'ts': datetime.datetime(2022, 4, 27, 9, 2, 15, 343000), 'temperature': 23.5, 'location': 1}, {'ts': datetime.datetime(2022, 4, 27, 9, 3, 15, 343000), 'temperature': 24.399999618530273, 'location': 1}]
# ANCHOR_END: query
conn
.
close
()
docs-examples/python/cursor_usage_native_reference.py
0 → 100644
浏览文件 @
8d15fc72
import
taos
conn
=
taos
.
connect
()
cursor
=
conn
.
cursor
()
cursor
.
execute
(
"DROP DATABASE IF EXISTS test"
)
cursor
.
execute
(
"CREATE DATABASE test"
)
cursor
.
execute
(
"USE test"
)
cursor
.
execute
(
"CREATE STABLE weather(ts TIMESTAMP, temperature FLOAT) TAGS (location INT)"
)
for
i
in
range
(
1000
):
location
=
str
(
i
%
10
)
tb
=
"t"
+
location
cursor
.
execute
(
f
"INSERT INTO
{
tb
}
USING weather TAGS(
{
location
}
) VALUES (now+
{
i
}
a, 23.5) (now+
{
i
+
1
}
a, 23.5)"
)
cursor
.
execute
(
"SELECT count(*) FROM weather"
)
data
=
cursor
.
fetchall
()
print
(
"count:"
,
data
[
0
][
0
])
cursor
.
execute
(
"SELECT tbname, * FROM weather LIMIT 2"
)
col_names
=
[
meta
[
0
]
for
meta
in
cursor
.
description
]
print
(
col_names
)
rows
=
cursor
.
fetchall
()
print
(
rows
)
cursor
.
close
()
conn
.
close
()
# output:
# count: 2000
# ['tbname', 'ts', 'temperature', 'location']
# row_count: -1
# [('t0', datetime.datetime(2022, 4, 27, 14, 54, 24, 392000), 23.5, 0), ('t0', datetime.datetime(2022, 4, 27, 14, 54, 24, 393000), 23.5, 0)]
docs-examples/python/handle_exception.py
0 → 100644
浏览文件 @
8d15fc72
import
taos
conn
:
taos
.
TaosConnection
=
None
try
:
conn
=
taos
.
connect
()
conn
.
execute
(
"CREATE TABLE 123"
)
# wrong sql
except
taos
.
Error
as
e
:
print
(
e
)
print
(
"exception class: "
,
e
.
__class__
.
__name__
)
print
(
"error number:"
,
e
.
errno
)
print
(
"error message:"
,
e
.
msg
)
except
BaseException
as
other
:
print
(
"exception occur"
)
print
(
other
)
finally
:
if
conn
is
not
None
:
conn
.
close
()
# output:
# [0x0216]: syntax error near 'Incomplete SQL statement'
# exception class: ProgrammingError
# error number: -2147483114
# error message: syntax error near 'Incomplete SQL statement'
docs-examples/python/result_set_examples.py
0 → 100644
浏览文件 @
8d15fc72
import
taos
conn
=
taos
.
connect
()
conn
.
execute
(
"DROP DATABASE IF EXISTS test"
)
conn
.
execute
(
"CREATE DATABASE test"
)
conn
.
select_db
(
"test"
)
conn
.
execute
(
"CREATE STABLE weather(ts TIMESTAMP, temperature FLOAT) TAGS (location INT)"
)
# prepare data
for
i
in
range
(
2000
):
location
=
str
(
i
%
10
)
tb
=
"t"
+
location
conn
.
execute
(
f
"INSERT INTO
{
tb
}
USING weather TAGS(
{
location
}
) VALUES (now+
{
i
}
a, 23.5) (now+
{
i
+
1
}
a, 23.5)"
)
result
:
taos
.
TaosResult
=
conn
.
query
(
"SELECT * FROM weather"
)
block_index
=
0
blocks
:
taos
.
TaosBlocks
=
result
.
blocks_iter
()
for
rows
,
length
in
blocks
:
print
(
"block "
,
block_index
,
" length"
,
length
)
print
(
"first row in this block:"
,
rows
[
0
])
block_index
+=
1
conn
.
close
()
# possible output:
# block 0 length 1200
# first row in this block: (datetime.datetime(2022, 4, 27, 15, 14, 52, 46000), 23.5, 0)
# block 1 length 1200
# first row in this block: (datetime.datetime(2022, 4, 27, 15, 14, 52, 76000), 23.5, 3)
# block 2 length 1200
# first row in this block: (datetime.datetime(2022, 4, 27, 15, 14, 52, 99000), 23.5, 6)
# block 3 length 400
# first row in this block: (datetime.datetime(2022, 4, 27, 15, 14, 52, 122000), 23.5, 9)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录