Integrating with Databend Cloud using databend-sqlalchemy
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.
Before You Start
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.
Step 1: Install Dependencies with pip
pip install databend-sqlalchemy
Step 2: Connect with databend_sqlalchemy
- Copy and paste the following code to the file
main.py
:
from sqlalchemy import create_engine, text
from sqlalchemy.engine.base import Connection, Engine
# Connecting to Databend Cloud with your credentials (replace PASSWORD, HOST, DATABASE, and WAREHOUSE_NAME)
engine = create_engine(
f"databend://{username}:{password}@{host_port_name}/{database_name}?sslmode=disable"
)
cursor = engine.connect()
cursor.execute(text('DROP TABLE IF EXISTS data'))
cursor.execute(text('CREATE TABLE IF NOT EXISTS data( Col1 TINYINT, Col2 VARCHAR )'))
cursor.execute(text("INSERT INTO data VALUES (1,'zz')"))
res = cursor.execute(text("SELECT * FROM data"))
print(res.fetchall())
- Run
python main.py
:
python main.py
[(1, 'zz')]