24. How to Load SQL Query Results into a pandas DataFrame
24.4 SQLAlchemy and DB-API: Beginner Patterns
Why. SQLite’s sqlite3 connection is enough for class. Real teams often pass a SQLAlchemy engine URL so MySQL and Postgres look similar. You do not need deep ORM knowledge — only “make engine → read_sql_query”.
What.
| Stack | Typical connectable | Notes |
|---|---|---|
| SQLite | sqlite3.connect(path) or sqlite:///file.db |
Best for learning |
| MySQL | SQLAlchemy URL + driver (e.g. pymysql) |
Needs server + user |
| PostgreSQL | SQLAlchemy URL + driver (e.g. psycopg2 / psycopg) |
Needs server + user |
| DB-API cursor | Connection with .cursor() |
pandas usually wants the connection / engine, not a raw cursor alone |
How.
Pick a stack and stay consistent
- Learning alone today → SQLite file (no install fight).
- Office MySQL/Postgres → ask IT for a read-only user and a staging database name.
pip install sqlalchemyonce in your venv; add the driver your DB needs.- Build an engine from a URL stored in an environment variable.
- Pass
engine(orengine.connect()) topd.read_sql_query. - Keep SQL text in a triple-quoted string or a
.sqlfile — not glued with random Python+.
Worked example A — SQLAlchemy + SQLite (same lab DB).
import pandas as pd
from sqlalchemy import create_engine
engine = create_engine("sqlite:///data/blinkit_lab.db")
df = pd.read_sql_query(
"SELECT city, status, amount_inr FROM orders LIMIT 5",
engine,
)
print(df)
engine.dispose()
Worked example B — MySQL and Postgres URL shapes (placeholders).
# Classroom shapes only — replace host/user/db; put passwords in env vars.
# pip install sqlalchemy pymysql # MySQL example
# pip install sqlalchemy psycopg2-binary # Postgres example
MYSQL_URL = "mysql+pymysql://readonly_user:SECRET@127.0.0.1:3306/blinkit_lab"
PG_URL = "postgresql+psycopg2://readonly_user:SECRET@127.0.0.1:5432/blinkit_lab"
# from sqlalchemy import create_engine
# engine = create_engine(os.environ["DS_DB_URL"])
# df = pd.read_sql_query("SELECT city, amount_inr FROM orders LIMIT 100", engine)
Worked example C — Compact comparison.
| Task | SQLite | MySQL / Postgres |
|---|---|---|
| Install for class | Built into Python | Server + driver package |
| Connection object | sqlite3.connect or SQLAlchemy |
Almost always SQLAlchemy URL |
| Limit rows while exploring | LIMIT 100 |
LIMIT 100 (MySQL/Postgres) |
| Same pandas after load | Yes | Yes |
Rani in Nagpur uses SQLite on the training laptop, then swaps only the URL when IT gives her a read-only Postgres user — the read_sql_query cell stays almost identical.
Ravindra Bagale's Tip
When you move from SQLite to another database, you mostly change only the connection URL, and your pandas code stays almost the same. Install the driver once, and keep the password in an environment variable, not in the code. Test every new query with LIMIT 5 first, and remove the limit only when the types and joins look right.
Ravindra Bagale's Tip – मराठी
SQLite वरून दुसऱ्या database वर जाताना बहुतेक फक्त connection URL बदलतो, आणि pandas चा code जवळपास तसाच राहतो. Driver एकदाच install करा, आणि password code मध्ये न ठेवता environment variable मध्ये ठेवा. प्रत्येक नवीन query आधी LIMIT 5 लावून test करा, आणि types आणि joins बरोबर दिसल्यावरच limit काढा.
Ravindra Bagale's Tip – हिंदी
SQLite से दूसरे database पर जाते समय ज़्यादातर सिर्फ़ connection URL बदलता है, और pandas का code लगभग वैसा ही रहता है। Driver एक बार install करो, और password code में नहीं बल्कि environment variable में रखो। हर नई query पहले LIMIT 5 लगाकर test करो, और types और joins सही दिखने पर ही limit हटाओ।