简体   繁体   English

sqlite3.OperationalError:靠近“WHERE”:语法错误(Python 2,sqlite3)

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

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