You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/developer/00-drivers/01-python.md
+82-66Lines changed: 82 additions & 66 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,14 +4,14 @@ title: Python
4
4
5
5
Databend offers the following Python packages enabling you to develop Python applications that interact with Databend:
6
6
7
-
-[databend-py (**Recommended**)](https://github.com/databendcloud/databend-py): Provides a direct interface to the Databend database. It allows you to perform standard Databend operations such as user login, database and table creation, data insertion/loading, and querying.
7
+
-[databend-driver (**Recommended**)](https://pypi.org/project/databend-driver/): A Python driver for Databend, providing both synchronous and asynchronous interfaces to interact with the Databend database, execute SQL queries, and handle data operations.
8
8
-[databend-sqlalchemy](https://github.com/databendcloud/databend-sqlalchemy): Provides a SQL toolkit and [Object-Relational Mapping](https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping) to interface with the Databend database. [SQLAlchemy](https://www.sqlalchemy.org/) is a popular SQL toolkit and ORM for Python, and databend-SQLAlchemy is a dialect for SQLAlchemy that allows you to use SQLAlchemy to interact with Databend.
9
9
10
-
Both packages require Python version 3.5 or higher. To check your Python version, run `python --version` in your command prompt. To install the latest `databend-py` or `databend-sqlalchemy` package:
10
+
Both packages require Python version 3.7 or higher. To check your Python version, run `python --version` in your command prompt. To install the latest `databend-driver` or `databend-sqlalchemy` package:
11
11
12
12
```bash
13
-
# install databend-py
14
-
pip install databend-py
13
+
# install databend-driver
14
+
pip install databend-driver
15
15
16
16
# install databend-sqlalchemy
17
17
pip install databend-sqlalchemy
@@ -47,9 +47,7 @@ This table illustrates the correspondence between Databend semi-structured data
47
47
| BITMAP | str |
48
48
| GEOMETRY | str |
49
49
50
-
In the following tutorial, you'll learn how to utilize the packages above to develop your Python applications. The tutorial will walk you through creating a SQL user in Databend and then writing Python code to create a table, insert data, and perform data queries.
51
-
52
-
## Tutorial-1: Integrating with Databend using Python
50
+
## Tutorial-1: Integrating with Self-Hosted Databend
53
51
54
52
Before you start, make sure you have successfully installed a local Databend. For detailed instructions, see [Local and Docker Deployments](/guides/deploy/deploy/non-production/deploying-local).
55
53
@@ -64,75 +62,63 @@ CREATE USER user1 IDENTIFIED BY 'abc123';
`databend-py` supports various parameters that can be configured either as URL parameters or as properties passed to the Client. The two examples provided below demonstrate equivalent ways of setting these parameters for the common DSN:
To create a valid DSN, select appropriate connection parameters outlined [here](https://github.com/databendcloud/databend-py/blob/main/docs/connection.md) based on your requirements.
90
-
91
-
### Step 3. Write a Python Program
65
+
### Step 2. Write a Python Program
92
66
93
67
In this step, you'll create a simple Python program that communicates with Databend. The program will involve tasks such as creating a table, inserting data, and executing data queries.
94
68
95
69
import Tabs from '@theme/Tabs';
96
70
import TabItem from '@theme/TabItem';
97
71
98
72
<TabsgroupId="python">
99
-
<TabItemvalue="databend-py"label="databend-py">
100
-
101
-
You will use the databend-py library to create a client instance and execute SQL queries directly.
_, results = client.execute("SELECT * FROM booklist")
124
-
for (title, author, date) in results:
125
-
print("{}{}{}".format(title, author, date))
126
-
client.execute('drop table booklist')
127
-
client.execute('drop database bookstore')
89
+
# Create a cursor to interact with Databend
90
+
cursor = client.cursor()
128
91
129
-
# Close Connect.
130
-
client.disconnect()
92
+
# Create database and use it
93
+
cursor.execute("CREATE DATABASE IF NOT EXISTS bookstore")
94
+
cursor.execute("USE bookstore")
95
+
96
+
# Create table
97
+
cursor.execute("CREATE TABLE IF NOT EXISTS booklist(title VARCHAR, author VARCHAR, date VARCHAR)")
98
+
99
+
# Insert data into the table
100
+
cursor.execute("INSERT INTO booklist VALUES('Readings in Database Systems', 'Michael Stonebraker', '2004')")
101
+
102
+
# Query data from the table
103
+
cursor.execute("SELECT * FROM booklist")
104
+
rows = cursor.fetchall()
105
+
106
+
# Print the results
107
+
for row in rows:
108
+
print(f"{row[0]}{row[1]}{row[2]}")
109
+
110
+
# Drop the table and database
111
+
cursor.execute('DROP TABLE booklist')
112
+
cursor.execute('DROP DATABASE bookstore')
113
+
114
+
# Close the cursor
115
+
cursor.close()
131
116
```
132
117
133
118
3. Run `python main.py`:
134
119
135
-
```text
120
+
```bash
121
+
python main.py
136
122
Readings in Database Systems Michael Stonebraker 2004
137
123
```
138
124
@@ -230,31 +216,61 @@ Readings in Database Systems Michael Stonebraker 2004
230
216
</TabItem>
231
217
</Tabs>
232
218
233
-
## Tutorial-2: Integrating with Databend Cloud using Python (databend-py)
219
+
## Tutorial-2: Integrating with Databend Cloud using databend-driver
234
220
235
221
Before you start, make sure you have successfully created a warehouse and obtained the connection information. For how to do that, see [Connecting to a Warehouse](/guides/cloud/using-databend-cloud/warehouses#connecting).
236
222
237
223
### Step 1. Install Dependencies with pip
238
224
239
225
```shell
240
-
pip install databend-py
226
+
pip install databend-driver
241
227
```
242
228
243
-
### Step 2. Connect with databend-py
229
+
### Step 2. Connect with databend-driver
230
+
231
+
1. Copy and paste the following code to the file `main.py`:
cursor.execute('CREATE TABLE IF NOT EXISTS data (x Int32, y String)')
247
+
248
+
# Describe the table
249
+
cursor.execute('DESC data')
250
+
251
+
# Insert data into the table
252
+
cursor.execute("INSERT INTO data (x, y) VALUES (1, 'yy'), (2, 'xx')")
253
+
254
+
# Select all data from the table
255
+
cursor.execute('SELECT * FROM data')
256
+
257
+
# Fetch all rows from the result
258
+
rows = cursor.fetchall()
259
+
260
+
# Print the result
261
+
for row in rows:
262
+
print(row.values())
263
+
```
264
+
265
+
2. Run `python main.py`:
266
+
267
+
```bash
268
+
python main.py
269
+
(1, 'yy')
270
+
(2, 'xx')
255
271
```
256
272
257
-
## Tutorial-3: Integrating with Databend Cloud using Python (databend-sqlalchemy)
273
+
## Tutorial-3: Integrating with Databend Cloud using databend-sqlalchemy
258
274
259
275
Before you start, make sure you have successfully created a warehouse and obtained the connection information. For how to do that, see [Connecting to a Warehouse](/guides/cloud/using-databend-cloud/warehouses#connecting).
title: Integrating with Databend Cloud using databend-driver
3
+
---
4
+
5
+
In this tutorial, we'll walk you through how to use the `databend-driver` to connect to Databend Cloud, create a table, insert data, and retrieve results with Python.
6
+
7
+
## Before You Start
8
+
9
+
Before you start, make sure you have successfully created a warehouse and obtained the connection information. For how to do that, see [Connecting to a Warehouse](/guides/cloud/using-databend-cloud/warehouses#connecting).
10
+
11
+
## Step 1: Install Dependencies with pip
12
+
13
+
```shell
14
+
pip install databend-driver
15
+
```
16
+
17
+
## Step 2: Connect with databend-driver
18
+
19
+
1. Copy and paste the following code to the file `main.py`:
20
+
21
+
```python
22
+
from databend_driver import BlockingDatabendClient
23
+
24
+
# Connecting to Databend Cloud with your credentials (replace PASSWORD, HOST, DATABASE, and WAREHOUSE_NAME)
title: Integrating with Databend Cloud using databend-sqlalchemy
3
+
---
4
+
5
+
In this tutorial, we'll walk you through how to use the `databend-sqlalchemy` library to connect to Databend Cloud, create a table, insert data, and query results using Python.
6
+
7
+
## Before You Start
8
+
9
+
Before you start, make sure you have successfully created a warehouse and obtained the connection information. For how to do that, see [Connecting to a Warehouse](/guides/cloud/using-databend-cloud/warehouses#connecting).
10
+
11
+
## Step 1: Install Dependencies with pip
12
+
13
+
```shell
14
+
pip install databend-sqlalchemy
15
+
```
16
+
17
+
## Step 2: Connect with databend_sqlalchemy
18
+
19
+
1. Copy and paste the following code to the file `main.py`:
20
+
21
+
```python
22
+
from databend_sqlalchemy import connector
23
+
24
+
# Connecting to Databend Cloud with your credentials (replace PASSWORD, HOST, DATABASE, and WAREHOUSE_NAME)
0 commit comments