MySQL update column with Python

Asmaro02 :

I am trying to update three columns in my table with data by using Python variables, but I have ran into an issue which I don't seem to understand.

I have done some amendments but still run into issues, can anyone see what I am doing wrong?

My table:

Columns   id   code    url       val1    val2    val3
Data      1    A2941   url.com   NULL    NULL    NULL

My query:

cursor.execute("UPDATE mytable SET val1=%s", (myVar))

Error message:

mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s' at line 1
Mureinik :

execute takes a tuple as its second argument. (myVar) is just the myVar variable surrounded in parenthesis. To create a tuple that contains only myVar, you need to add a comma:

cursor.execute("UPDATE mytable SET val1=%s", (myVar,))
# Here --------------------------------------------^

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=371519&siteId=1