Python pysqlite 不接受我的 qmark 参数化
我认为我是个傻瓜,也许没有导入正确的包,但是当我这样做时......
from pysqlite2 import dbapi2 as sqlite
import types
import re
import sys
...
def create_asgn(self):
stmt = "CREATE TABLE ? (login CHAR(8) PRIMARY KEY NOT NULL, grade INTEGER NOT NULL)"
stmt2 = "insert into asgn values ('?', ?)"
self.cursor.execute(stmt, (sys.argv[2],))
self.cursor.execute(stmt2, [sys.argv[2], sys.argv[3]])
...
I get the error pysqlite2.dbapi2.OperationalError: near "?": syntax error
这对我来说没有什么意义,因为文档显示 pysqlite 是 qmark 参数化的。 我对 python 和 db-api 很陌生,请帮助我! 谢谢
I think I am being a bonehead, maybe not importing the right package, but when I do...
from pysqlite2 import dbapi2 as sqlite
import types
import re
import sys
...
def create_asgn(self):
stmt = "CREATE TABLE ? (login CHAR(8) PRIMARY KEY NOT NULL, grade INTEGER NOT NULL)"
stmt2 = "insert into asgn values ('?', ?)"
self.cursor.execute(stmt, (sys.argv[2],))
self.cursor.execute(stmt2, [sys.argv[2], sys.argv[3]])
...
I get the error pysqlite2.dbapi2.OperationalError: near "?": syntax error
This makes very little sense to me, as the docs show that pysqlite is qmark parametrized. I am new to python and db-api though, help me out! THANKS
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为参数只能传递给 VALUES。 表名无法参数化。
另外,第二个查询的参数化参数周围有引号。 删除引号,转义由下划线库自动为您处理。
That's because parameters can only be passed to VALUES. The table name can't be parametrized.
Also you have quotes around a parametrized argument on the second query. Remove the quotes, escaping is handled by the underlining library automatically for you.
尝试删除分配给 stmt2 的行中的引号:
此外,正如 nosklo 所说,您不能在 CREATE TABLE 语句中使用问号参数化。 直接将表名粘贴到 SQL 中。
Try removing the quotes in the line that assigns to
stmt2
:Also, as nosklo says, you can't use question-mark parameterisation with CREATE TABLE statements. Stick the table name into the SQL directly.
如果你真的想这样做,请尝试这样的事情:
def read(db="projects"):
If you really want to do it, try something like this:
def read(db="projects"):