Python2.7-SQLite3库输出错误消息;sqlite3.0错误:接近“0”&引用;:语法错误“;
代码如下。如何用变量值替换Python2.7-SQLite3库输出错误消息;sqlite3.0错误:接近“0”&引用;:语法错误“;,python,sqlite,Python,Sqlite,代码如下。如何用变量值替换?,[表,url] 预期的SQL命令是select*from OTHER_URL,其中URL=”http://a.com/a.jpg“ 此SQL命令在sqlite3命令行界面上不会发生错误 import sqlite3 from contextlib import closing dbname = "ng.db" with closing(sqlite3.connect(dbname)) as conn: c = conn.cursor() c.ex
?
,[表,url]
预期的SQL命令是select*from OTHER_URL,其中URL=”http://a.com/a.jpg“
此SQL命令在sqlite3命令行界面上不会发生错误
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()
这里有两个错误。首先,不能对表名(或列名)使用参数替换,只能对值使用参数替换。您需要对任何其他内容使用字符串插值 其次,不需要在value参数周围加引号;换人会处理好的 因此:
这里有两个错误。首先,不能对表名(或列名)使用参数替换,只能对值使用参数替换。您需要对任何其他内容使用字符串插值 其次,不需要在value参数周围加引号;换人会处理好的 因此:
c.execute('select * from {} where url=?'.format(table), [url])