0

I am running a script that does some queries to a database. Those queries are made of strings that come in a Json package from GroupMe's API. This is the error I'm getting:

x.execute("""SELECT url FROM BIN WHERE name=%s""",(str(messageData['text'].lower())))
  File "/home/User2/.local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 187, in execute
query = query % tuple([db.literal(item) for item in args])
TypeError: not all arguments converted during string formatting

The program is a GroupMe Bot that prints a link for a gif when it gets mentioned in a message. So if I were to send the message "ohNo.gif" the bot would look on the DB for the imgur link that's related to "ohNo.gif" and then print it. Apparently there is something wrong with my string conversion.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Robert
  • 11
  • 1
  • 1

1 Answers1

0

Query parameters must be passed in a tuple (note comma):

x.execute("SELECT url FROM BIN WHERE name=%s", (messageData['text'].lower(), ))

This (str(messageData['text'].lower()) is not of a tuple type - it's a string:

>>> params = ('test')
>>> type(params)
<type 'str'>
>>> params = ('test', )
>>> type(params)            
<type 'tuple'>

Also, you don't need triple quotes and call str().

Also see:

Hope that helps.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Isn't it beautiful? a single comma fixed it. Thank you so much. Would you please explain what you mean by "passed in a tuple"? – Robert Jan 16 '14 at 05:17
  • This is actually a popular problem here, see [this](http://stackoverflow.com/questions/10401873/mysql-and-python-select-statement-issues) and [docs](http://mysql-python.sourceforge.net/MySQLdb.html) (see "some examples" section). – alecxe Jan 16 '14 at 05:22