user3142695
user3142695

Reputation: 17372

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?

Upvotes: 0

Views: 318

Answers (2)

MattDMo
MattDMo

Reputation: 102942

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.

Upvotes: 2

miku
miku

Reputation: 188224

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

Upvotes: 1

Related Questions