"?"附近的 Python SQLite : syntax error

标签 python sqlite

另一个问题。我正在尝试:

self.table = 'table'
a = 'column'
b = 'value'

c.execute('INSERT INTO ? (?) VALUES (?)', (self.table, a, b))

但是我得到了

<class 'sqlite3.OperationalError'>:near "?": syntax error

同时,

c.execute('INSERT INTO {0} ({1}) VALUES ({2})'.format(self.table, a, b))

工作完美,除了安全问题。

我知道我不是第一个遇到这个问题的人,但我还没有找到适合我的解决方案。

最佳答案

表名、列名不能参数化。请尝试跟随。

self.table = 'table'
b = 'value'

c.execute('INSERT INTO {} ({}) VALUES (?)'.format(self.table, a), (b,))

关于 "?"附近的 Python SQLite : syntax error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18159352/

相关文章:

java - 将数据存储到sqlite

python - Amazon EC2 和 S3 何时使用 Python/SQLite?

python - Pandas df.to_csv ("file.csv"encode ="utf-8")仍然为减号提供垃圾字符

python - Python不抛出错误或异常,websocket on_message

python - 将数据传递到 Django 表单中

mysql - 根据行数与行数除以where条件的结果对所有记录进行排序

python - 如果 ID 出现多次,Pandas 选择行

python - 如何在 Django 1.6 中使用 HTTP POST 请求接收 json 数据?

sql - 带联接和组的SQLite SELECT?

python - 使用 SQLAlchemy 动态获取列名