0

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
1
  • Do you have any details on the error message? What does sys.argv[1] equal before you insert it in the SQL? Commented Jul 5, 2014 at 6:06

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.