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
+8-236
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 Databend, 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,236 +47,8 @@ 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.
50
+
## Tutorials
51
51
52
-
## Tutorial-1: Integrating with Databend using Python
53
-
54
-
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
-
56
-
### Step 1. Prepare a SQL User Account
57
-
58
-
To connect your program to Databend and execute SQL operations, you must provide a SQL user account with appropriate privileges in your code. Create one in Databend if needed, and ensure that the SQL user has only the necessary privileges for security.
59
-
60
-
This tutorial uses a SQL user named 'user1' with password 'abc123' as an example. As the program will write data into Databend, the user needs ALL privileges. For how to manage SQL users and their privileges, see [User & Role](/sql/sql-commands/ddl/user/).
`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
92
-
93
-
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
-
95
-
import Tabs from '@theme/Tabs';
96
-
import TabItem from '@theme/TabItem';
97
-
98
-
<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.
102
-
103
-
1. Install databend-py.
104
-
105
-
```shell
106
-
pip install databend-py
107
-
```
108
-
109
-
2. Copy and paste the following code to the file `main.py`:
110
-
111
-
```python title='main.py'
112
-
from databend_py import Client
113
-
114
-
# Connecting to a local Databend with a SQL user named 'user1' and password 'abc123' as an example.
115
-
# Feel free to use your own values while maintaining the same format.
116
-
# Setting secure=False means the client will connect to Databend using HTTP instead of HTTPS.
conn.execute("CREATE DATABASE IF NOT EXISTS bookstore")
160
-
conn.execute("USE bookstore")
161
-
conn.execute("CREATE TABLE IF NOT EXISTS booklist(title VARCHAR, author VARCHAR, date VARCHAR)")
162
-
conn.execute("INSERT INTO booklist VALUES('Readings in Database Systems', 'Michael Stonebraker', '2004')")
163
-
conn.execute('SELECT * FROM booklist')
164
-
165
-
results = conn.fetchall()
166
-
for (title, author, date) in results:
167
-
print("{}{}{}".format(title, author, date))
168
-
conn.execute('drop table booklist')
169
-
conn.execute('drop database bookstore')
170
-
171
-
# Close Connect.
172
-
conn.close()
173
-
```
174
-
175
-
3. Run `python main.py`:
176
-
177
-
```text
178
-
Readings in Database Systems Michael Stonebraker 2004
179
-
```
180
-
181
-
</TabItem>
182
-
183
-
<TabItemvalue="databend-sqlalchemy with Engine"label="databend-sqlalchemy (Engine)">
184
-
185
-
You will use the databend-sqlalchemy library to create an engine instance and execute SQL queries using the connect method to create connections that can execute queries.
186
-
187
-
1. Install databend-sqlalchemy.
188
-
189
-
```shell
190
-
pip install databend-sqlalchemy
191
-
```
192
-
193
-
2. Copy and paste the following code to the file `main.py`:
194
-
195
-
```python title='main.py'
196
-
from sqlalchemy import create_engine, text
197
-
198
-
# Connecting to a local Databend with a SQL user named 'user1' and password 'abc123' as an example.
199
-
# Feel free to use your own values while maintaining the same format.
200
-
# Setting secure=False means the client will connect to Databend using HTTP instead of HTTPS.
connection1.execute(text("CREATE DATABASE IF NOT EXISTS bookstore"))
208
-
connection1.execute(text("USE bookstore"))
209
-
connection1.execute(text("CREATE TABLE IF NOT EXISTS booklist(title VARCHAR, author VARCHAR, date VARCHAR)"))
210
-
connection1.execute(text("INSERT INTO booklist VALUES('Readings in Database Systems', 'Michael Stonebraker', '2004')"))
211
-
212
-
result = connection2.execute(text("SELECT * FROM booklist"))
213
-
results = result.fetchall()
214
-
215
-
for (title, author, date) in results:
216
-
print("{}{}{}".format(title, author, date))
217
-
218
-
# Close Connect.
219
-
connection1.close()
220
-
connection2.close()
221
-
engine.dispose()
222
-
```
223
-
224
-
3. Run `python main.py`:
225
-
226
-
```text
227
-
Readings in Database Systems Michael Stonebraker 2004
228
-
```
229
-
230
-
</TabItem>
231
-
</Tabs>
232
-
233
-
## Tutorial-2: Integrating with Databend Cloud using Python (databend-py)
234
-
235
-
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).
client.execute('CREATE TABLE if not exists data (x Int32,y VARCHAR)')
251
-
client.execute('DESC data')
252
-
client.execute("INSERTINTO data (Col1,Col2) VALUES", [1, 'yy', 2, 'xx'])
253
-
_, res= client.execute('select * from data')
254
-
print(res)
255
-
```
256
-
257
-
## Tutorial-3: Integrating with Databend Cloud using Python (databend-sqlalchemy)
258
-
259
-
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).
cursor.execute('CREATE TABLE IF NOT EXISTS data( Col1 TINYINT, Col2 VARCHAR )')
275
-
cursor.execute("INSERTINTO data (Col1,Col2) VALUES", [1, 'yy', 2, 'xx'])
276
-
cursor.execute("SELECT * FROM data")
277
-
print(cursor.fetchall())
278
-
```
279
-
280
-
:::tip
281
-
Replace `{USER}, {PASSWORD}, {HOST}, {WAREHOUSE_NAME} and {DATABASE}`in the code with your connection information. For how to obtain the connection information, see [Connecting to a Warehouse](/guides/cloud/using-databend-cloud/warehouses#connecting).
282
-
:::
52
+
-[Integrating with Databend Cloud using databend-driver](/tutorials/programming/python/integrating-with-databend-cloud-using-databend-driver)
53
+
-[Integrating with Databend Cloud using databend-sqlalchemy](/tutorials/programming/python/integrating-with-databend-cloud-using-databend-sqlalchemy)
54
+
-[Integrating with Self-Hosted Databend](/tutorials/programming/python/integrating-with-self-hosted-databend)
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