简体   繁体   中英

Sql-statement with variables in python

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?

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.

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,))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM