🎓 All Courses | 📚 Python Programming Syllabus

📋 Study this course on TaskLoco
#python-programming#sqlite#database

SQLite — Built-in Database

Python ships with SQLite3. No installation needed for simple databases.


import sqlite3

# Connect (creates file if not exists)
conn = sqlite3.connect("mydb.sqlite")
cursor = conn.cursor()

Create table & insert:

cursor.execute("""
    CREATE TABLE IF NOT EXISTS users (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        name TEXT NOT NULL,
        email TEXT UNIQUE
    )
""")

cursor.execute(
    "INSERT INTO users (name, email) VALUES (?, ?)",
    ("Alice", "[email protected]")
)
conn.commit()

Query:

cursor.execute("SELECT * FROM users WHERE name = ?", ("Alice",))
rows = cursor.fetchall()
for row in rows:
    print(row)

Always use ? placeholders — never f-strings in SQL queries. That's how SQL injection attacks happen.


Close connection:

conn.close()
# Or use context manager:
with sqlite3.connect("mydb.sqlite") as conn:

YouTube • Top 10
Python Programming: Database Access with SQLite
Tap to Watch ›
📸
Google Images • Top 10
Python Programming: Database Access with SQLite
Tap to View ›

Reference:

Wikipedia: SQLite

image for linkhttps://en.wikipedia.org/wiki/SQLite

📚 Python Programming — Full Course Syllabus
📋 Study this course on TaskLoco

TaskLoco™ — The Sticky Note GOAT