How to Connect with Python
Utilize Python drivers to ensure seamless integration with CrateDB through the SQLAlchemy dialect, enabling smooth operation across various platforms. Trusted by numerous tools within the Python ecosystem, it efficiently supports CPython and PyPy for hassle-free interactions, including compatibility with the Crash CLI.
# pip install crate
from crate import client
conn = client.connect("https://scarlet-corde.aks1.eastus2.azure.cratedb.net:4200",
username="admin", password="", verify_ssl_cert=True)
with conn:
cursor = conn.cursor()
cursor.execute("SELECT name FROM sys.cluster")
result = cursor.fetchone()
print(result)
# pip install crate[sqlalchemy]
import sqlalchemy as sa
dburi = "crate://admin:@scarlet-corde.aks1.eastus2.azure.cratedb.net:4200?ssl=true"
engine = sa.create_engine(dburi, echo=True)
with engine.connect() as conn:
with conn.execute(sa.text("SELECT name FROM sys.cluster")) as cursor:
print(cursor.fetchone())
# pip install crate[sqlalchemy] pandas
import pandas as pd
import sqlalchemy as sa
dburi = "crate://admin:@scarlet-corde.aks1.eastus2.azure.cratedb.net:4200?ssl=true"
engine = sa.create_engine(dburi, echo=True)
with engine.connect() as conn:
df = pd.read_sql(sql=sa.text("SELECT * FROM sys.summits"), con=conn)
df.info()
print(df)
The Python driver acts as an implementation compatible with the Python Database API 2.0 specification, providing essential functionalities to establish connections, execute SQL queries, and manage query results seamlessly within CrateDB. Moreover, it includes the CrateDB SQLAlchemy dialect, enabling a comprehensive integration with SQLAlchemy, a popular SQL toolkit for Python. For detailed technical specifications and comprehensive functionalities, explore the CrateDB Python driver documentation.