python how to write an update sql query?

430 Views Asked by At

I am trying to write an update query but I could not manage the string. My connections is ok. My query is like this:

str='hello'
cursor.execute('UPDATE users SET message = '+str+' WHERE UserId=13')

This is giving me error: undefined column name 'hello'. I want to update message column as hello but it is getting it as a column name. In sql, when I write it as UPDATE users SET message = 'hello' WHERE UserId=13 it works but I could not figured out how should I write my query like that in python. How should I write my query?

2

There are 2 best solutions below

0
maya On BEST ANSWER

try it:

cursor.execute('UPDATE users SET message="{}" WHERE UserId=13'.format("hello"))
0
Majed Jaber On

try this:

str='hello'
cursor.execute("UPDATE users SET message = '" + str + "' WHERE UserId=13")