Python Client
Opteryx offers Python bindings compliant with a subset of the PEP 249 DBAPI specification, allowing integration into Python applications, scripts, and Jupyter notebooks.
Basic Usage
This section demonstrates how to establish a connection to Opteryx, execute a query, and fetch results.
import opteryx
# Establish a connection
conn = opteryx.connect()
# Create a cursor object
cursor = conn.cursor()
# Execute a SQL query
cursor.execute("SELECT * FROM $planets;")
# Fetch all rows
rows = cursor.fetchall()
Using Parameterized Queries
Parameterized queries help mitigate SQL injection risks by safely incorporating user input into SQL statements.
import opteryx
# Establish a connection
conn = opteryx.connect()
# Create a cursor object
cursor = conn.cursor()
# Execute a SQL query
cursor.execute("SELECT * FROM $planets WHERE id = :user_provided_id;",
{ "user_provided_id": 1 }
)
# Fetch all rows
rows = cursor.fetchall()
Handling Query Results
Opteryx returns query results as Orso DataFrames accessible through the cursor. You can interact with the results in multiple ways:
- Iteration: Loop over the cursor directly.
- Fetching Methods: Utilize
fetchone()
,fetchmany(size)
, andfetchall()
for specific result sets. - Conversion Routines:
arrow()
for an Arrow Tablepandas()
for a pandas DataFramepolars()
for a Polars DataFrame
Simplified Short-Form API
For convenience, Opteryx provides a streamlined API that abstracts connection and cursor creation, suitable for quick queries.
Basic Short-Form Usage
Execute a SQL query and immediately fetch all results:
import opteryx
# Execute a SQL query and get the results
cursor = opteryx.query("SELECT * FROM $planets;").fetchall()