Skip to main content

How to Put Python Values into a SQLite Query with ? Placeholders

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
connection.execute("<SQL with a ? for each value>", (<value>, <value>, ...))
connection.execute("<SQL with one ?>", (<value>,))
PartWhat 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​

placeholders.py
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 placeholders.py script in a macOS-style code window: the title The Gardener's Year is inserted with VALUES (?, ?) and the tuple title, 2021, then found again with WHERE title = ? and the one-item tuple title comma.

  • 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 SELECT has one placeholder, so it gets a one-item tuple: (title,), with the comma.

Step 2: Run It​

Terminal
uv run python placeholders.py
Output
("The Gardener's Year", 2021)

The output in a terminal: the tuple with the title The Gardener's Year and 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​

MistakeWhat happensDo this instead
Building the SQL with an f-string, f"... VALUES ('{title}', 2021)"The apostrophe ends the string early: sqlite3.OperationalError: near "s": syntax errorUse ? 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 ? marksIncorrect 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 errorPlaceholders are only for values; write names in the SQL

Troubleshooting​

SymptomLikely causeFix
ProgrammingError: Incorrect number of bindings supplied with a large numberA single value wasn't wrapped in a tuple, so its characters were countedAdd the comma: (value,)
OperationalError: near "...": syntax error only for some inputsA value was pasted into the SQL textSwitch 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​