[英]sqlite3.OperationalError: near “WHERE”: syntax error (Python 2, sqlite3)
I can't seem to get past the following error: 我似乎无法通过以下错误:
Traceback (most recent call last):
File "datascraper.py", line 352, in <module>
URL))
sqlite3.OperationalError: near "WHERE": syntax error
It comes from the following code (line 352 marked): 它来自以下代码(标记为352行):
Table = someSQLtable //Has columns (providername, [other columns], providerlink)
SQLDatabase = sqlite3.connect(someSQLDatabase.db)
DBControl = cursor(SQLDatabase)
Name = 'somestring'
URL = 'http://www.someurl.com/stuff/'
[...] # Random other lines
DBControl.execute('''INSERT INTO '''+Table+''' (providername, providerlink)
VALUES (?, ?) WHERE NOT EXISTS (
SELECT * FROM '''+Table+'''
WHERE '''+Table+'''.providerlink = ?
);
352) ''', (Name, URL, URL))
For reference, the SQL commands wrapped up in the Python should look like this: 作为参考,Python中包含的SQL命令应该如下所示:
INSERT INTO someSQLtable (providername, providerlink)
VALUES ('somestring', 'http://www.someurl.com/stuff/')
WHERE NOT EXISTS (
SELECT * FROM someSQLtable
WHERE someSQLtable.providerlink = 'http://www.someurl.com/stuff/')
And my goal is to check if the table already contains the entry in question by checking if the freshly retrieved link (which is unique) to the table, then writing to the table if it isn't already there. 我的目标是检查表中是否已经包含有问题的条目,方法是检查表中是否有新查找的链接(对于表是唯一的),然后写入表中(如果它尚未存在)。
Playing with whitespace shows that the exception comes up for the last URL on line 352. 使用空格显示第352行的最后一个URL出现异常。
I've already tried modifying the input to another string to see if that works, changing the code to use Python's string ops (horror!), and having a drink. 我已经尝试将输入修改为另一个字符串以查看是否有效,将代码更改为使用Python的字符串操作(恐怖!),然后喝一杯。 Nothing seems to work so far. 到目前为止似乎没有任何工作。
The INSERT statement has no WHERE clause. INSERT语句没有WHERE子句。
If you have a UNIQUE constraint (and if not, you should create one) on the providerLink
column, you can simply use INSERT OR IGNORE : 如果您在providerLink
列上有UNIQUE约束(如果没有,则应创建一个),您只需使用INSERT 或IGNORE :
INSERT OR IGNORE INTO someSQLtable(providername, providerlink)
VALUES ('somestring', 'http://www.someurl.com/stuff/')
Otherwise, you have to replace the VALUES clause with a query: 否则,您必须使用查询替换VALUES子句:
INSERT INTO someSQLtable(providername, providerlink)
SELECT 'somestring', 'http://www.someurl.com/stuff/'
WHERE NOT EXISTS (
SELECT * FROM someSQLtable
WHERE providerlink = 'http://www.someurl.com/stuff/')
Perhaps it's different i sqllite, but normally you cant have a where clause on a values statement. 也许它与我的sqllite不同,但通常你不能在一个值语句中有一个where子句。 Try something like: 尝试类似的东西:
INSERT INTO someSQLtable (providername, providerlink)
SELECT 'somestring', 'http://www.someurl.com/stuff/'
FROM ( values (1) ) as T
WHERE NOT EXISTS (
SELECT * FROM someSQLtable
WHERE someSQLtable.providerlink = 'http://www.someurl.com/stuff/')
If ( values (1) ) as T is not supported, perhaps there is something similar to Oracles dual or DB2's sysibm.sysdummy1 如果(value(1))不支持T,那么可能有类似于Oracles dual或DB2的sysibm.sysdummy1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.