Skip to content

Commit 3b3a235

Browse files
committed
Add connection based example in README
1 parent 3e171cb commit 3b3a235

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,42 @@ python3 -m chdb "SELECT 1,'abc'" Pretty
6060
### Data Input
6161
The following methods are available to access on-disk and in-memory data formats:
6262

63+
<details>
64+
<summary><h4>🗂️ Connection based API (recommended)</h4></summary>
65+
66+
```python
67+
import chdb
68+
69+
# Create a connection (in-memory by default)
70+
conn = chdb.connect(":memory:")
71+
# Or use file-based: conn = chdb.connect("test.db")
72+
73+
# Create a cursor
74+
cur = conn.cursor()
75+
76+
# Execute queries
77+
cur.execute("SELECT number, toString(number) as str FROM system.numbers LIMIT 3")
78+
79+
# Fetch data in different ways
80+
print(cur.fetchone()) # Single row: (0, '0')
81+
print(cur.fetchmany(2)) # Multiple rows: ((1, '1'), (2, '2'))
82+
83+
# Get column information
84+
print(cur.column_names()) # ['number', 'str']
85+
print(cur.column_types()) # ['UInt64', 'String']
86+
87+
# Use the cursor as an iterator
88+
cur.execute("SELECT number FROM system.numbers LIMIT 3")
89+
for row in cur:
90+
print(row)
91+
92+
# Always close resources when done
93+
cur.close()
94+
conn.close()
95+
```
96+
</details>
97+
98+
6399
<details>
64100
<summary><h4>🗂️ Query On File</h4> (Parquet, CSV, JSON, Arrow, ORC and 60+)</summary>
65101

0 commit comments

Comments
 (0)