Sumod
Sumod

Reputation: 3846

Sqlite3 - Update table using Python code - syntax error near %s

This is my Python code -

cursor.execute("""UPDATE tasks SET task_owner=%s,task_remaining_hours=%s,                      task_impediments=%s,task_notes=%s WHERE task_id=%s""",                      (new_task_owner,new_task_remaining_hours,new_task_impediments,
                      new_task_notes,task_id))

This is the SQL statement I try in SQLite3 manager (Firefox extension)

UPDATE tasks SET task_owner=%s,task_remaining_hours=%d,task_impediments=%s,task_notes=%s WHERE task_id=%d,("sumod",10,"none","test",1)   

The error I get is -

sqlite3.OperationalError: near "%": syntax error

I have tried many web searches including SO, tutorials and self-troubleshooting, but this error does not go away. What exactly I am doing wrong here.

Upvotes: 15

Views: 34226

Answers (2)

DevB2F
DevB2F

Reputation: 5075

It is also possible to use %s:

cursor.execute("UPDATE tasks SET task_owner='%s', task_remaining_hours='%s', 
task_impediments = '%s', task_notes = '%s' WHERE task_id= '%s' " % 
(new_task_owner,new_task_remaining_hours,new_task_impediments,new_task_notes,task_id))

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270607

I believe Python's SQLite implementation uses ? placeholders, unlike MySQLdb's %s. Review the documentation.

cursor.execute("""UPDATE tasks SET task_owner = ? ,task_remaining_hours = ?,task_impediments = ?,task_notes = ? WHERE task_id= ? """,
  (new_task_owner,new_task_remaining_hours,new_task_impediments,new_task_notes,task_id))

Upvotes: 31

Related Questions