user3465521
user3465521

Reputation:

Inserting python variables into MySQL syntax problems

Can someone tell me what's wrong with this Syntax?

>>>y = 14
>>> cursor.execute("INSERT INTO accounts VALUES ('Bobby', %s)", (y))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/mysql/connector/cursor.py", line 477, in execute
    stmt = operation % self._process_params(params)
  File "/Library/Python/2.7/site-packages/mysql/connector/cursor.py", line 355, in      
_process_params
    "Failed processing format-parameters; %s" % err)
    mysql.connector.errors.ProgrammingError: Failed processing format-parameters; argument   
    2 to map() must support iteration

Upvotes: 2

Views: 2639

Answers (1)

alecxe
alecxe

Reputation: 474131

y should be inside a tuple:

cursor.execute("INSERT INTO accounts VALUES ('Bobby', %s)", (y, ))

FYI, (y) is not a tuple - it is an int in parenthesis, adding comma (y,) makes it a tuple with one element inside:

>>> y = 14
>>> type((y))
<type 'int'>
>>> type((y,))
<type 'tuple'>

Upvotes: 7

Related Questions