Skip to content

Commit fd1396e

Browse files
authored
docs: improve python driver api usage examples (#3002)
1 parent 8f963c7 commit fd1396e

File tree

4 files changed

+80
-8
lines changed

4 files changed

+80
-8
lines changed

docs/cn/developer/00-drivers/01-python.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,42 @@ cursor.execute("CREATE TABLE users (id INT, name STRING)")
4242
# 写入:插入数据
4343
cursor.execute("INSERT INTO users VALUES (?, ?)", (1, 'Alice'))
4444

45+
# 查询:读取数据
4546
# 查询:读取数据
4647
cursor.execute("SELECT * FROM users")
48+
49+
# 获取列名
50+
# cursor.description 返回一个元组列表,其中每个元组的第一个元素是列名
51+
print(f"Columns: {[desc[0] for desc in cursor.description]}")
52+
4753
for row in cursor.fetchall():
48-
print(row.values())
54+
# row 是一个 databend_driver.Row 对象
55+
# 通过列名访问
56+
print(f"id: {row['id']}, name: {row['name']}")
4957

5058
cursor.close()
5159
```
5260

61+
### Row 对象使用
62+
63+
`Row` 对象支持多种访问模式和方法:
64+
65+
```python
66+
for row in cursor.fetchall():
67+
# 1. 通过列名访问(推荐)
68+
print(f"Name: {row['name']}")
69+
70+
# 2. 通过索引访问
71+
print(f"First column: {row[0]}")
72+
73+
# 3. 转换为元组
74+
print(f"Values: {row.values()}")
75+
76+
# 4. 显式方法调用
77+
print(row.get_by_field('name'))
78+
print(row.get_by_index(0))
79+
```
80+
5381
### 异步用法
5482

5583
```python

docs/cn/tutorials/develop/python/integrating-with-databend-cloud-using-databend-driver.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,23 @@ cursor.execute('SELECT * FROM data')
4343
# 读取所有结果
4444
rows = cursor.fetchall()
4545

46+
# 获取列名
47+
# cursor.description 返回一个元组列表,其中每个元组的第一个元素是列名
48+
column_names = [desc[0] for desc in cursor.description]
49+
print(f"Columns: {column_names}")
50+
4651
# 打印结果
4752
for row in rows:
48-
print(row.values())
53+
# row 是一个 databend_driver.Row 对象
54+
# 通过列名访问
55+
print(f"x: {row['x']}, y: {row['y']}")
4956
```
5057

5158
2. 执行 `python main.py`
5259

5360
```bash
5461
python main.py
55-
(1, 'yy')
56-
(2, 'xx')
62+
Columns: ['x', 'y']
63+
x: 1, y: yy
64+
x: 2, y: xx
5765
```

docs/en/developer/00-drivers/01-python.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,42 @@ cursor.execute("CREATE TABLE users (id INT, name STRING)")
4242
# Write: Insert data
4343
cursor.execute("INSERT INTO users VALUES (?, ?)", (1, 'Alice'))
4444

45+
# Query: Read data
4546
# Query: Read data
4647
cursor.execute("SELECT * FROM users")
48+
49+
# Get column names
50+
# cursor.description returns a list of tuples, where the first element is the column name
51+
print(f"Columns: {[desc[0] for desc in cursor.description]}")
52+
4753
for row in cursor.fetchall():
48-
print(row.values())
54+
# row is a databend_driver.Row object
55+
# Access by column name
56+
print(f"id: {row['id']}, name: {row['name']}")
4957

5058
cursor.close()
5159
```
5260

61+
### Working with Row Objects
62+
63+
The `Row` object supports multiple access patterns and methods:
64+
65+
```python
66+
for row in cursor.fetchall():
67+
# 1. Access by column name (Recommended)
68+
print(f"Name: {row['name']}")
69+
70+
# 2. Access by index
71+
print(f"First column: {row[0]}")
72+
73+
# 3. Convert to tuple
74+
print(f"Values: {row.values()}")
75+
76+
# 4. Explicit methods
77+
print(row.get_by_field('name'))
78+
print(row.get_by_index(0))
79+
```
80+
5381
### Asynchronous Usage
5482

5583
```python

docs/en/tutorials/develop/python/integrating-with-databend-cloud-using-databend-driver.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,23 @@ cursor.execute('SELECT * FROM data')
4242
# Fetch all rows from the result
4343
rows = cursor.fetchall()
4444

45+
# Get column names from cursor.description
46+
# cursor.description returns a list of tuples, where the first element is the column name
47+
column_names = [desc[0] for desc in cursor.description]
48+
print(f"Columns: {column_names}")
49+
4550
# Print the result
4651
for row in rows:
47-
print(row.values())
52+
# row is a databend_driver.Row object
53+
# Access by column name
54+
print(f"x: {row['x']}, y: {row['y']}")
4855
```
4956

5057
2. Run `python main.py`:
5158

5259
```bash
5360
python main.py
54-
(1, 'yy')
55-
(2, 'xx')
61+
Columns: ['x', 'y']
62+
x: 1, y: yy
63+
x: 2, y: xx
5664
```

0 commit comments

Comments
 (0)