24. How to Load SQL Query Results into a pandas DataFrame
24.2 Connection Basics: Read-Only Mindset
Why. One wrong UPDATE or DELETE from a notebook can hurt a live Blinkit-style ops table. Learners practise read first. Treat production like a museum: look, do not rearrange the exhibits.
What.
- A connection is a live pipe from Python to the database.
- Prefer a read-only DB user when your admin can create one.
- Keep host, user, and password out of Git — use environment variables or a local untracked file.
- For learning on a laptop, SQLite (one
.dbfile) is safest: no server, easy reset.
How.
Safe habit before any query
- Ask: is this a practice DB, a staging copy, or production?
- If production: use a read-only account; never run
UPDATE/DELETE/DROPfrom analysis notebooks. - Start with
SELECT+LIMIT(orTOP/FETCH FIRST) while exploring. - Store secrets in environment variables (
os.environ), not in shared.ipynbcells you screenshot. - Close or dispose engines when a long script finishes (or use
withwhere the driver supports it).
Worked example A — SQLite classroom file (no password).
import sqlite3
from pathlib import Path
db_path = Path("data/blinkit_lab.db")
db_path.parent.mkdir(parents=True, exist_ok=True)
con = sqlite3.connect(db_path)
print("connected", db_path.resolve())
con.close()
Worked example B — Secrets via environment (shape only).
import os
# Set once in the terminal (example — use YOUR practice values):
# export DS_DB_URL="sqlite:///data/blinkit_lab.db"
url = os.environ.get("DS_DB_URL", "sqlite:///data/blinkit_lab.db")
print("using", url.split("@")[-1]) # avoid printing passwords
Worked example C — Who gets which access (fictional ops).
| Person | Access |
|---|---|
| Ravindra Bagale (analyst learner) | Read-only on orders, stores |
| DBA / backend | Write rights on production |
| Shared training laptop | Local SQLite copy only |
Ravindra Bagale's Tip
Practise on a small SQLite file first. While you explore data, use a read-only login, because a login that can DELETE rows can break production by mistake. Never paste a production password into a notebook, because even a screenshot of it can end up on WhatsApp.
Ravindra Bagale's Tip – मराठी
आधी एका छोट्या SQLite file वर सराव करा. Data explore करताना read-only login वापरा, कारण DELETE करू शकणारा login चुकून production बिघडवू शकतो. Production चा password कधीही notebook मध्ये paste करू नका, कारण त्याचा एक screenshot सुद्धा WhatsApp वर पोहोचू शकतो.
Ravindra Bagale's Tip – हिंदी
पहले एक छोटी SQLite file पर practice करो। Data explore करते समय read-only login इस्तेमाल करो, क्योंकि DELETE कर सकने वाला login गलती से production बिगाड़ सकता है। Production का password कभी notebook में paste मत करो, क्योंकि उसका एक screenshot भी WhatsApp तक पहुँच सकता है।