Testing man Testing man - 2 months ago 6
Python Question

sqlite3 update database with "?" - python

I have:

database testing.db3
table: mytable
columns: 'name', 'status'

I do:

con = sqlite3.connect('testing.db3')
cur = con.cursor()
cur.execute('select * from mytable where status is null')

data = cur.fetchone()
print(data[0])


as wanted I receive following result:

('Johnny', 'None')


Now I am trying to update this line, it works if I do:

cur.execute('UPDATE mytable SET status = "Online" WHERE name is "Johnny"')


However if I want to update it with '?', it just doesn't work. Any idea why?

cur.execute('UPDATE mytable SET status = "Online" WHERE name is ?', data[0])
con.commit()
con.close()


Would appreciate any help. Thank you.

Answer
cur.execute('UPDATE mytable SET status = "Online" WHERE name is ?', (data[0], ))

execute expects a tuple as an input parameter. Therefore, even if you insert only a single value, you should format it as a tuple.

Although in your question you say that data[0] = ('Johnny', 'None'), in the comments you indicate that data[0] is the first value of the tuple. So I assume the latter one is the case.