0

Having trouble here, It worked when I had multiple (%s,%s) and data was (user,pass) or something like that.

However with the following code I keep getting this error.

query = query % tuple([db.literal(item) for item in args])
TypeError: not all arguments converted during string formatting

Why does this keep happening? It only occurs when there is only a single argument

This code is from my flask application

 username = request.values['username']
 update_stmt = (
 "UPDATE ACCOUNTS SET IN_USE = 1 WHERE USER = '(%s)'"
 )
 data = (username)
 cursor.execute(update_stmt,data)
Jean-François Fabre
141k24 gold badges179 silver badges246 bronze badges
asked Jan 11, 2017 at 12:23
1
  • Is this sorted out now? Commented Feb 15, 2017 at 23:38

1 Answer 1

4

For a single valued tuple to be recognized as a tuple you need a trailing ,

data = (username,)

And unrelated, you don't really need to quote in your query

"UPDATE ACCOUNTS SET IN_USE = 1 WHERE USER = (%s)"

Your full code should be

username = request.values['username']
update_stmt = (
 "UPDATE ACCOUNTS SET IN_USE = 1 WHERE USER = (%s)"
)
data = (username,)
cursor.execute(update_stmt,data)
answered Jan 11, 2017 at 12:25
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks! Are you sure I do not need the quotes? Its an email address with a @ in it that was throwing errors if not in quotes, either way I shall try :) Thanks again
well, i have never used quotes in a prepared statement but your comment caused self doubt and I checked the docs :-) the examples here don't have quotes either: dev.mysql.com/doc/connector-python/en/…
Does this look simpler? Its giving me an error but for one value it seems more readable update_stmt = ("UPDATE ACCOUNTS SET IN_USE = 1 WHERE USER = '%s'" % username) cursor.execute(update_stmt)
Super similar error though :/ ValueError: need more than 1 value to unpack
noooooo don't ever use % string formatting in sql that's unsafe.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.