即使没有语法错误,Sqlite 语法错误也是如此。帮助?
这是我用来更新 sqlite 数据库中的信息的代码:
self.c.execute("UPDATE proxydata (proxy, description) VALUES ('" + proxy + "', '" + description + "') WHERE proxy='" + proxy + "'")
但我收到此错误:
sqlite3.OperationalError: near "(": syntax error
在我的一生中,我找不到错误。执行时两个变量都是格式正确的字符串。
编辑:
这工作正常:
self.c.execute("UPDATE proxydata SET description='" + description + "' WHERE proxy='" + proxy + "'")
您可以关闭线程。
Here's the code I use to update information in my sqlite database:
self.c.execute("UPDATE proxydata (proxy, description) VALUES ('" + proxy + "', '" + description + "') WHERE proxy='" + proxy + "'")
But I get this error:
sqlite3.OperationalError: near "(": syntax error
For the life of mine I can't find an error. Both variables upon execution are correctly formated strings.
EDIT:
This works fine:
self.c.execute("UPDATE proxydata SET description='" + description + "' WHERE proxy='" + proxy + "'")
You can close the thread.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用参数化 sql:
这显然更容易,因为您不必自己引用参数,因此更不容易出错。
它也更安全,因为参数化 sql 允许 sqlite3 防止 sql 注入。
请注意,如果
proxy
或description
本身包含单引号,则需要对其进行转义。您手动构造的 SQL 语句没有正确转义该类型的引号。这可能是您看到的语法错误的原因。编辑:正如其他人所指出的,语法错误的真正根源很简单,
UPDATE ... VALUES ... WHERE
不是有效的(sqlite)SQL。 正确的 UPDATE 语法 是UPDATE ... SET ... WHERE
。Use a parametrized sql:
This is clearly easier, since you don't have to quote the arguments yourself, and thus less error-prone.
It is also safer, since parametrizing the sql allows sqlite3 to protect against sql injection.
Note if
proxy
ordescription
itself contains a single quotation mark, then it would need to be escaped. Your manual construction of the SQL statement does not properly escape that type of quotation mark. It might be the cause of the syntax error you are seeing.Edit: As others have noted, the real source of the syntax error is simply that
UPDATE ... VALUES ... WHERE
is not valid (sqlite) SQL. The proper UPDATE syntax isUPDATE ... SET ... WHERE
.没有语法错误就没有语法错误。试试这个:
这里描述了此语法: http://www.sqlite.org/lang_update.html
There's no such thing as a syntax error without a syntax error. Try this:
This syntax is described here: http://www.sqlite.org/lang_update.html