How to Work With Databend in Python
Before You Begin
- Databend : Make sure Databend is running and accessible, see How to deploy Databend.
- How to Create User
- How to Grant Privileges to User
Create Databend User
mysql -h127.0.0.1 -uroot -P3307
Create a User
CREATE USER user1 IDENTIFIED BY 'abc123';
Grants Privileges
Grants ALL
privileges to the user user1
:
GRANT ALL ON *.* TO user1;
Python
This guideline show how to connect and query to Databend using Python.
We will be creating a table named books
and insert a row, then query it.
Using databend-py
pip install databend-py
main.py
#!/usr/bin/env python3
from databend_py import Client
client = Client('user1:abc123@127.0.0.1', port=8000, secure=False)
# Create database, table.
client.execute("CREATE DATABASE IF NOT EXISTS book_db")
client.execute("USE book_db")
client.execute("CREATE TABLE IF NOT EXISTS books(title VARCHAR, author VARCHAR, date VARCHAR)")
# Insert new book.
client.execute("INSERT INTO books VALUES('mybook', 'author', '2022')")
# Query.
_, results = client.execute("SELECT * FROM books")
for (title, author, date) in results:
print("{} {} {}".format(title, author, date))
client.execute('drop table books')
client.execute('drop database book_db')
Run python main.py
:
mybook author 2022
Using databend-sqlalchemy
pip install databend-sqlalchemy
main.py
#!/usr/bin/env python3
from databend_sqlalchemy import connector
conn = connector.connect(f"http://user1:abc123@127.0.0.1:8000").cursor()
conn.execute("CREATE DATABASE IF NOT EXISTS book_db")
conn.execute("USE book_db")
conn.execute("CREATE TABLE IF NOT EXISTS books(title VARCHAR, author VARCHAR, date VARCHAR)")
conn.execute("INSERT INTO books VALUES('mybook', 'author', '2022')")
conn.execute('SELECT * FROM books')
results = conn.fetchall()
for result in results:
print(result)
conn.execute('drop table books')
conn.execute('drop database book_db')
Run python main.py
:
('mybook', 'author', '2022')