P
P
Pickto2016-07-23 09:37:02
Python
Pickto, 2016-07-23 09:37:02

How to add variables to UPDATE query in sqlite?

Here is my code, making a simple application.

sql = 'SELECT money FROM base WHERE nickname LIKE "%s"'% username
cursor.execute(sql)
for x in cursor.execute(sql):
    money = x[0]
money += 1
sql = 'UPDATE base SET money=? WHERE nickname LIKE ?', (money, username)
 cursor.execute(sql)

Gives an error message:
Traceback (most recent call last):
  File "C:/Users/Admin/Desktop/Bot/bot.py", line 74, in <module>
    main()
  File "C:/Users/Admin/Desktop/Bot/bot.py", line 57, in main
    cursor.execute(sql)
  File "d:\build\apsw\apsw-3.12.2-r1\src\cursor.c", line 1019, in APSWCursor_execute.sqlite3_prepare
TypeError: coercing to Unicode: need string or buffer, tuple found

What should I do about it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2016-07-23
@Pickto

sql = 'UPDATE base SET money=? WHERE nickname LIKE ?', (money, username)
cursor.execute(*sql)

# OR
sql = 'UPDATE base SET money=? WHERE nickname LIKE ?'
data = (money, username)
cursor.execute(sql, data)

read about args kwargs
read the manual for using execute

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question