k9b
k9b

Reputation: 1493

Error with string formatting?

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)

Upvotes: 0

Views: 46

Answers (1)

e4c5
e4c5

Reputation: 53734

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)

Upvotes: 4

Related Questions