Skip to content
Advertisement

python how to write an update sql query?

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?

Advertisement

Answer

try it:

cursor.execute('UPDATE users SET message="{}" WHERE UserId=13'.format("hello"))
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement