Skip to content

Instantly share code, notes, and snippets.

@mcsee
Last active February 11, 2026 02:15
Show Gist options
  • Select an option

  • Save mcsee/066db12ea24de4afed7f11452b5b03fa to your computer and use it in GitHub Desktop.

Select an option

Save mcsee/066db12ea24de4afed7f11452b5b03fa to your computer and use it in GitHub Desktop.
from typing import Optional
import sqlite3
class DatabaseManager:
def __init__(self, db_path: str):
self.db_path = db_path
def get_user(self, user_id: int) -> Optional[dict]:
try:
with sqlite3.connect(self.db_path) as conn:
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
cursor.execute("SELECT * FROM users WHERE id = ?", (user_id,))
row = cursor.fetchone()
return dict(row) if row else None
except sqlite3.Error as e:
print(f"Database error: {e}")
return None
db = DatabaseManager("app.db")
user = db.get_user(123)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment