How to Put Python Values into a SQLite Query with ? Placeholders
Real queries use values from your program: the title a user typed, the email of a customer. Gluing those into the SQL text breaks on the first apostrophe, and lets a crafted value rewrite your query. Placeholders are the fix: you write ? where each value goes and pass the values separately. This guide inserts and finds a title with an apostrophe in it, with real output from Python 3.15.2.
Syntax
connection.execute("<SQL with a ? for each value>", (<value>, <value>, ...))
connection.execute("<SQL with one ?>", (<value>,))
| Part | What it means |
|---|---|
? | A placeholder: where one value goes. Not inside quotes |
(<value>, <value>, ...) | The values, in the same order as the ? marks |
(<value>,) | A tuple of one value. The comma makes it a tuple |
Python's sqlite3 documentation warns: "beware of using Python's string operations to assemble queries, as they are vulnerable to SQL injection attacks." Its answer: "Instead, use the DB-API's parameter substitution." For ? placeholders, "parameters must be a sequence whose length must match the number of placeholders, or a ProgrammingError is raised."
Prerequisites
- uv and Python. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
import sqlite3
connection = sqlite3.connect(":memory:")
connection.execute("CREATE TABLE books (title TEXT, year INTEGER)")
title = "The Gardener's Year"
connection.execute("INSERT INTO books (title, year) VALUES (?, ?)", (title, 2021))
row = connection.execute("SELECT title, year FROM books WHERE title = ?", (title,)).fetchone()
print(row)
connection.close()

- The title holds an apostrophe, the same character SQL uses to end a string.
VALUES (?, ?)has two placeholders, and(title, 2021)gives two values, in that order.- The
SELECThas one placeholder, so it gets a one-item tuple:(title,), with the comma.
Step 2: Run It
uv run python placeholders.py
("The Gardener's Year", 2021)

The title went in and came back with its apostrophe intact. sqlite3 sent the values to SQLite separately from the SQL text, so the apostrophe was never read as SQL.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Building the SQL with an f-string, f"... VALUES ('{title}', 2021)" | The apostrophe ends the string early: sqlite3.OperationalError: near "s": syntax error | Use ? and pass the value |
(title) for one value, without the comma | (title) is just the string, so each character counts as a value: Incorrect number of bindings supplied. The current statement uses 1, and there are 19 supplied. | Write (title,) |
Fewer values than ? marks | Incorrect number of bindings supplied. The current statement uses 2, and there are 1 supplied. | Pass one value per ? |
A placeholder for a table or column name, SELECT * FROM ? | sqlite3.OperationalError: near "?": syntax error | Placeholders are only for values; write names in the SQL |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
ProgrammingError: Incorrect number of bindings supplied with a large number | A single value wasn't wrapped in a tuple, so its characters were counted | Add the comma: (value,) |
OperationalError: near "...": syntax error only for some inputs | A value was pasted into the SQL text | Switch that value to a placeholder |
FAQ
Can I name the placeholders?
Yes. Write :title in the SQL and pass a dict: connection.execute("SELECT year FROM books WHERE title = :title", {"title": title}) works the same way.
Do placeholders work with numbers and None?
Yes. Any value sqlite3 can store goes through a placeholder, such as the year 2021 above.
Conclusion
With ? in the SQL and the values in a tuple, the title with an apostrophe went in and came back unchanged, where an f-string gave a syntax error.
Additional Resources
- How to use placeholders to bind values in SQL queries —
?and named placeholders, and why string formatting is unsafe. - Cursor.execute — the
parametersargument.
