What object do you need to execute SQL commands?
The Cursor object. You create it from your connection: cursor = conn.cursor()
What is the primary method used to run an SQL string?
The execute() method on the cursor object.
Example: Executing a simple SELECT query
sql_query = "SELECT * FROM users"`\n`cursor.execute(sql_query)`
How do you safely pass Python variables into your SQL query?
Use placeholders in the SQL string and pass the variables as a second argument (usually a tuple) to execute().
What placeholder is used by sqlite3?
The question mark (?). SELECT * FROM users WHERE name = ?
What placeholder is common in libraries like psycopg2 (PostgreSQL) and mysql-connector?
The %s placeholder. SELECT * FROM users WHERE name = %s
Example: Safe execution with a variable (sqlite3)
user_name = "Alice"\ncursor.execute("SELECT * FROM users WHERE name = ?", (user_name,))
Why should you never use f-strings or string formatting (like +) to build queries?
It creates a major security vulnerability called SQL Injection.
After executing a SELECT query, how do you retrieve a single row?
row = cursor.fetchone()
After executing a SELECT query, how do you retrieve all matching rows?
rows = cursor.fetchall() (This returns a list of tuples)
After executing a SELECT query, how do you retrieve a specific number of rows?
rows = cursor.fetchmany(5) (Fetches 5 rows at a time)
What must you call on the connection object to finalize changes like INSERT, UPDATE, or DELETE?
You must call conn.commit().
Example: Executing an INSERT statement
cursor.execute("INSERT INTO users (name, email) VALUES (?, ?)", ("Bob", "bob@example.com"))\nconn.commit()
What happens if you forget to call conn.commit() after an INSERT?
The data will not be saved to the database. The transaction will be rolled back.
What method can you call to undo changes made since the last commit?
conn.rollback()
What should you do with the cursor after you are finished with it?
Close it to free up resources: cursor.close()
What is the executemany() method used for?
To efficiently run the same SQL command multiple times with different sets of data (e.g., bulk inserting many rows from a list).
Example: Using executemany()
users_to_add = [("Charlie", "c@ex.com"), ("David", "d@ex.com")]\ncursor.executemany("INSERT INTO users (name, email) VALUES (?, ?)", users_to_add)\nconn.commit()
What does cursor.rowcount tell you?
The number of rows affected by the last DML command (INSERT, UPDATE, DELETE) or the number of rows returned by a query (behavior can vary by driver).
What is the with statement used for in this context?
It acts as a context manager, typically for the connection, to automatically commit() on success or rollback() on error, and often ensures the connection is closed.
Example: Using with for a connection (recommended)
with sqlite3.connect('app.db') as conn:\n cursor = conn.cursor()\n cursor.execute(...)\n # No conn.commit() needed here if used as context manager
ORM example
from sqlalchemy import create_engine, select
from sqlalchemy.orm import sessionmaker
from models import User
Create engine and session
engine = create_engine('sqlite:///:memory:')
Session = sessionmaker(bind=engine)
session = Session()
Execute query
bob = session.query(User).filter_by(name='Bob').first()What does ORM stand for?
Object-Relational Mapper
What is the primary goal of an ORM?
To map Python classes/objects to database tables/rows, allowing interaction without writing raw SQL.