Python pysqlite 不接受我的 qmark 参数化 [英] Python pysqlite not accepting my qmark parameterization

查看:35
本文介绍了Python pysqlite 不接受我的 qmark 参数化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我是个笨蛋,可能没有导入正确的包,但是当我这样做时...

<预><代码>从 pysqlite2 导入 dbapi2 作为 sqlite进口类型进口重新导入系统...def create_asgn(self):stmt = "CREATE TABLE ? (登录 CHAR(8) PRIMARY KEY NOT NULL, Grade INTEGER NOT NULL)"stmt2 = "插入 asgn 值 ('?', ?)"self.cursor.execute(stmt, (sys.argv[2],))self.cursor.execute(stmt2, [sys.argv[2], sys.argv[3]])... 我收到错误 pysqlite2.dbapi2.OperationalError: near "?": syntax error

这对我来说意义不大,因为文档显示 pysqlite 是 qmark 参数化的.不过,我是 python 和 db-api 的新手,请帮帮我!谢谢

解决方案

那是因为参数只能传递给 VALUES.表名无法参数化.

此外,您在第二个查询中的参数化参数周围有引号.删除引号,转义由下划线库自动为您处理.

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

解决方案

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.

这篇关于Python pysqlite 不接受我的 qmark 参数化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆