Ravindra BagaleCourses & study guides मराठी Track your progress

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 .db file) is safest: no server, easy reset.

How.

Safe habit before any query

  1. Ask: is this a practice DB, a staging copy, or production?
  2. If production: use a read-only account; never run UPDATE / DELETE / DROP from analysis notebooks.
  3. Start with SELECT + LIMIT (or TOP / FETCH FIRST) while exploring.
  4. Store secrets in environment variables (os.environ), not in shared .ipynb cells you screenshot.
  5. Close or dispose engines when a long script finishes (or use with where 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

Pehle SQLite lab. Production password notebook madhe paste naka — screenshot pan WhatsApp la jailato.