简体   繁体   English

Python2.7-SQLite3库输出错误消息“ sqlite3.OperationalError:靠近“?”:语法错误”

[英]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.

相关问题 sqlite3.OperationalError:靠近“WHERE”:语法错误(Python 2,sqlite3) - sqlite3.OperationalError: near “WHERE”: syntax error (Python 2, sqlite3) Python-sqlite3 sqlite3.OperationalError:接近“%”:语法错误? - Python - sqlite3 sqlite3.OperationalError: near “%”: syntax error? SQLite3 Python 2.7 sqlite3.OperationalError语法错误 - SQLite3 Python 2.7 sqlite3.OperationalError syntax error python sqlite3.OperationalError:“-”附近:语法错误 - python sqlite3.OperationalError: near “-”: syntax error sqlite3.OperationalError:“,”附近:语法错误python - sqlite3.OperationalError: near “,”: syntax error python Python和sqlite3抛出错误:sqlite3.OperationalError:near“s”:语法错误 - Python and sqlite3 throwing an error: sqlite3.OperationalError: near “s”: syntax error Python SQlite3更新函数sqlite3.OperationalError:“ WHERE”附近:语法错误 - Python SQlite3 Update function, sqlite3.OperationalError: near “WHERE”: syntax error Python:sqlite3.OperationalError:在“ &lt;”附近:语法错误(使用HTML源代码更新sqlite3字段) - Python: sqlite3.OperationalError: near “<”: syntax error (updating sqlite3 field with html source code) sqlite3“ OperationalError:附近”)“:语法错误” python - sqlite3 “OperationalError: near ”)“: syntax error” python sqlite3“OperationalError:near”(“:语法错误”python - sqlite3 “OperationalError: near ”(“: syntax error” python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM