cursor.execute() rejects a lone value that is not wrapped in a tuple
Fix it fast
Most common cause, 1 of 3Parentheses around one number, with no comma
Write the comma after the single value, or hand over a one-item list. psycopg2, psycopg 3 and Connector/Python all accept both shapes.
cur.execute("SELECT name FROM users WHERE id = %s", (user_id,))
# equally valid
cur.execute("SELECT name FROM users WHERE id = %s", [user_id])Short answer
TypeError: 'int' object does not support indexing (psycopg2), "query parameters should be a sequence or a mapping, got int" (psycopg 3) and "Could not process parameters: int(5), it must be of type list, tuple or dict" (MySQL Connector/Python) all mean execute() got a bare value. (5) is just 5; pass (5,) or [5]. Applies to psycopg2 2.9, psycopg 3.3, Connector/Python 26.x.
Which cause do you have?
Print the type of the object you hand to execute() as its second argument, on the line just before the call. int or str points to the first two causes; a list whose first item is an int points to executemany().
params = (user_id)
print(type(params).__name__, repr(params))
# int 42 -> no tuple was built
# tuple (42,) -> the params are fineThen find the line you see in this list and open its fix:
- 1Parentheses around one number, with no comma
'int' object does not support indexing, got int, or Could not process parameters: int(...) - 2A string passed on its own as the parameters
got str (psycopg 3), or Could not process parameters: str(...) (Connector/Python) - 3executemany() was given a flat list of values
The traceback ends in executemany(), and the list you passed looks like [3, 7, 9]
What does the error look like?
TypeError: 'int' object does not support indexingTypeError: query parameters should be a sequence or a mapping, got intmysql.connector.errors.ProgrammingError: Could not process parameters: int(5), it must be of type list, tuple or dictCould not process parameters: str(alice@example.com), it must be of type list, tuple or dict
Cause 1 of 3
Parentheses around one number, with no comma
You see'int' object does not support indexing, got int, or Could not process parameters: int(...)
Check
The 'int' object does not support indexing error in psycopg2, and the int(...) form of Could not process parameters in Connector/Python, show up when the params argument is a plain number. Python treats (user_id) as a grouped expression, so the value stays an int; a tuple of one only exists once a comma follows it. psycopg2 asks that number for its item 0 to fill the first %s, and Python refuses.
>>> type((5))
<class 'int'>
>>> type((5,))
<class 'tuple'>Fix
Write the comma after the single value, or hand over a one-item list. psycopg2, psycopg 3 and Connector/Python all accept both shapes.
cur.execute("SELECT name FROM users WHERE id = %s", (user_id,))
# equally valid
cur.execute("SELECT name FROM users WHERE id = %s", [user_id])Verify
The query runs and returns the expected row instead of raising.
print(cur.fetchone())Confirmed by psycopg.org
Cause 2 of 3
A string passed on its own as the parameters
You seegot str (psycopg 3), or Could not process parameters: str(...) (Connector/Python)
Check
psycopg 3 and Connector/Python turn down a bare string in the params slot with the same family of errors. psycopg 3 leaves str and bytes out of what it counts as a parameter sequence, and Connector/Python only takes a list, a tuple or a dict, so an email address or a name passed without a tuple fails before any SQL is sent.
email = "alice@example.com"
cur.execute("SELECT id FROM users WHERE email = %s", (email)) # still a strFix
Wrap the string in a one-item tuple or list, exactly as with a number.
cur.execute("SELECT id FROM users WHERE email = %s", (email,))Verify
The lookup returns the matching id rows.
print(cur.fetchall())Confirmed by psycopg.org
Cause 3 of 3
executemany() was given a flat list of values
You seeThe traceback ends in executemany(), and the list you passed looks like [3, 7, 9]
Check
With executemany() the same TypeError comes from the rows, not from the list. The method expects one tuple or mapping per row and runs the statement once for each, so [3, 7, 9] gives it three bare integers. psycopg2 then reports that an int does not support indexing, psycopg 3 says got int, and Connector/Python, which sends UPDATE and DELETE statements through execute() row by row, says Could not process parameters.
print(type(user_ids).__name__, type(user_ids[0]).__name__)
# list int -> rows are bare valuesFix
Turn every value into its own one-item tuple before the call.
rows = [(uid,) for uid in user_ids]
cur.executemany("DELETE FROM sessions WHERE user_id = %s", rows)
conn.commit()Verify
With psycopg2, cur.rowcount holds the total rows affected across all the parameter sets.
print(cur.rowcount)Confirmed by psycopg.org
How do I stop it coming back?
- Build the params on their own line (params = (user_id,)) and pass the variable, so a missing comma is visible in code review.
- Annotate the helper that wraps execute() with params: tuple[Any, ...] | dict[str, Any] and run mypy in CI; passing an int or a str to it then fails the build.
- For queries with more than one value, use named placeholders like %(user_id)s with a dict, which has no comma to forget.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: ShopGo, TaxiDex, QistLock.