Hint: Remember: separate data from code to stop injection [OK]
Common Mistakes:
Thinking parameterized queries speed up queries
Believing users can safely write SQL commands
Confusing parameterized queries with table creation
2. Which of the following is the correct way to use parameterized queries with Flask's database cursor?
easy
A. cursor.execute(f"SELECT * FROM users WHERE id = {user_id}")
B. cursor.execute("SELECT * FROM users WHERE id = ?", (user_id,))
C. cursor.execute("SELECT * FROM users WHERE id = %s", (user_id,))
D. cursor.execute("SELECT * FROM users WHERE id = :id", user_id)
Solution
Step 1: Identify correct placeholder syntax
In Flask with many DB adapters like psycopg2, %s is used as placeholder for parameters.
Step 2: Check parameter passing format
Parameters must be passed as a tuple, e.g., (user_id,). cursor.execute("SELECT * FROM users WHERE id = %s", (user_id,)) uses %s and tuple correctly.
Final Answer:
cursor.execute("SELECT * FROM users WHERE id = %s", (user_id,)) -> Option C
Quick Check:
Use %s with tuple for parameters [OK]
Hint: Use %s and tuple for parameters in execute() [OK]
Common Mistakes:
Using f-strings directly with user input
Using ? placeholder which is for SQLite, not psycopg2
Passing parameters incorrectly without tuple
3. Given this Flask code snippet, what will be the output if user_id = "1 OR 1=1" and parameterized queries are used correctly?
cursor.execute("SELECT * FROM users WHERE id = %s", (user_id,))
results = cursor.fetchall()
print(results)
medium
A. Only rows where id equals the string '1 OR 1=1' will be returned
B. All rows from users table will be returned
C. Syntax error due to SQL injection
D. No rows will be returned because of wrong query
Solution
Step 1: Understand parameterized query effect
Parameterized queries treat user input as data, not code, so "1 OR 1=1" is a string value, not SQL logic.
Step 2: Result of query with injected string
The query looks for rows where id equals the string "1 OR 1=1", which likely matches no rows or only exact matches.
Final Answer:
Only rows where id equals the string '1 OR 1=1' will be returned -> Option A
Quick Check:
Parameterized input treated as data = Only rows where id equals the string '1 OR 1=1' will be returned [OK]
Hint: Parameterized input is data, not code, so no injection [OK]
Common Mistakes:
Assuming injection runs and returns all rows
Expecting syntax error from parameterized query
Thinking query fails due to input format
4. Identify the error in this Flask code that tries to prevent SQL injection:
user_id = request.args.get('id')
cursor.execute("SELECT * FROM users WHERE id = %s" % user_id)
results = cursor.fetchall()
medium
A. Using wrong placeholder %s instead of ?
B. Missing parentheses in fetchall()
C. Not converting user_id to integer
D. Using string formatting instead of parameterized query
Solution
Step 1: Check how query is constructed
The code uses string formatting (% operator) to insert user_id directly into the query string.
Step 2: Why this is wrong for injection prevention
Direct string formatting allows malicious input to change SQL code, causing SQL injection risk.
Final Answer:
Using string formatting instead of parameterized query -> Option D
Quick Check:
Use parameterized queries, not string formatting [OK]
Hint: Never build queries with string formatting; use parameters [OK]
Common Mistakes:
Thinking fetchall() needs parentheses fix
Believing user_id must be int for injection safety
Confusing %s and ? placeholders
5. You want to safely insert multiple user records into a database using Flask and prevent SQL injection. Which approach correctly combines parameterized queries with batch insertion?