I'm trying to execute a sql-statement via python. But this doesn't work:
cursor.execute("UPDATE setting set foo=%s WHERE bar=%s", 4, str(sys.argv[1]))
This also doesn't work:
t = ("4",sys.argv[1])
cursor.execute('UPDATE setting set foo=? WHERE bar=?', t)
But this one works:
cursor.execute('UPDATE setting set foo=%s WHERE bar="something"', 3)
What am I doing wrong?
asked Jul 5, 2014 at 6:03
user3142695
17.5k56 gold badges205 silver badges379 bronze badges
-
Do you have any details on the error message? What does sys.argv[1] equal before you insert it in the SQL?Luke Peterson– Luke Peterson2014年07月05日 06:06:19 +00:00Commented Jul 5, 2014 at 6:06
2 Answers 2
Try running
cursor.execute("UPDATE setting set foo=%s WHERE bar=%s", (4, str(sys.argv[1])))
The arguments need to be passed as a tuple if there are more than one.
answered Jul 5, 2014 at 6:07
MattDMo
103k21 gold badges251 silver badges239 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
I think you need to pass the parameters as a tuple, even if there is only one:
cur.execute("insert into people values (?, ?)", (who, age))
Or:
cur.execute("insert into people values (?, 100)", (who,))
answered Jul 5, 2014 at 6:07
miku
189k47 gold badges314 silver badges317 bronze badges
Comments
default