Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Databases and Redis

cursor.execute() rejects a lone value that is not wrapped in a tuple

Your error

TypeError: 'int' object does not support indexing3 more lines it can show

Fix it fast

Most common cause, 1 of 3

Parentheses 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.

Fix
cur.execute("SELECT name FROM users WHERE id = %s", (user_id,))
# equally valid
cur.execute("SELECT name FROM users WHERE id = %s", [user_id])
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to psycopg2 2.9.13, psycopg 3.3.9, mysql-connector-python 26.10.0, Python 3.11 to 3.15
  • Updated
  • By Maaz Zindani, Senior Software Engineer

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().

Run this first
params = (user_id)
print(type(params).__name__, repr(params))
# int 42      -> no tuple was built
# tuple (42,) -> the params are fine

Then find the line you see in this list and open its fix:

  1. 1Parentheses around one number, with no comma'int' object does not support indexing, got int, or Could not process parameters: int(...)
  2. 2A string passed on its own as the parametersgot str (psycopg 3), or Could not process parameters: str(...) (Connector/Python)
  3. 3executemany() was given a flat list of valuesThe 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 indexing
  • TypeError: query parameters should be a sequence or a mapping, got int
  • mysql.connector.errors.ProgrammingError: Could not process parameters: int(5), it must be of type list, tuple or dict
  • Could 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.

Check
>>> 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.

Fix
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.

Verify
print(cur.fetchone())

Confirmed by psycopg.org

Did this fix it?

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.

Check
email = "alice@example.com"
cur.execute("SELECT id FROM users WHERE email = %s", (email))  # still a str

Fix

Wrap the string in a one-item tuple or list, exactly as with a number.

Fix
cur.execute("SELECT id FROM users WHERE email = %s", (email,))

Verify

The lookup returns the matching id rows.

Verify
print(cur.fetchall())

Confirmed by psycopg.org

Did this fix it?

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.

Check
print(type(user_ids).__name__, type(user_ids[0]).__name__)
# list int  -> rows are bare values

Fix

Turn every value into its own one-item tuple before the call.

Fix
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.

Verify
print(cur.rowcount)

Confirmed by psycopg.org

Did this fix it?

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.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.