Sqlite3-使用Python代码更新表-在%s附近出现语法错误

2024-05-19 21:14:02 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的Python代码-

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

这是我在SQLite3管理器(Firefox扩展)中尝试的SQL语句

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)   

我得到的错误是-

sqlite3.OperationalError: near "%": syntax error

我尝试过很多网络搜索,包括SO、教程和自我故障排除,但这个错误并没有消失。我到底做错了什么。


Tags: 代码idnewtask错误updatewherecursor
2条回答

也可以使用%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))

我相信Python的SQLite实现使用?占位符,这与MySQLdb的%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))

相关问题 更多 >