[英]Python2.7 - SQLite3 library outputs error message “sqlite3.OperationalError: near ”?“: syntax error”
Code is follow. 代码如下。 How to get replaced
?
如何更换
?
by value of variables [table, url]
? 按变量
[table, url]
?
Expected SQL command is select * from OTHER_URL where url="http://a.com/a.jpg"
期望的SQL命令是
select * from OTHER_URL where url="http://a.com/a.jpg"
This SQL command occurs no error on the sqlite3 command line interface. 在sqlite3命令行界面上,此SQL命令不会发生任何错误。
import sqlite3
from contextlib import closing
dbname = "ng.db"
with closing(sqlite3.connect(dbname)) as conn:
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS OTHER_URL (url TEXT)")
conn.commit()
table = "OTHER_URL"
url = "http://a.com/a.jpg"
with closing(sqlite3.connect(dbname)) as conn:
c = conn.cursor()
c.execute('select * from ? where url="?"', [table, url])
print c.fetchone()
There are two errors here. 这里有两个错误。 Firstly, you can't use parameter substitution for table names (or column names), only for values.
首先,不能将参数替换用于表名(或列名),而只能用于值。 You need to use string interpolation for anything else.
您需要对其他任何内容使用字符串插值。
Secondly, you don't need quotes around the value parameter; 其次,您不需要在value参数周围加上引号; the substitution will take care of that.
替换会解决这个问题。
So: 所以:
c.execute('select * from {} where url=?'.format(table), [url])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.