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?

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.

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

Comments

Your Answer

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.