Sqlite3.OperationalError: next to "WHERE": syntax error (Python 2, sqlite3)

I can not miss 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 is marked):

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, SQL commands completed in Python should look like this:

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 specified record, checking if the link just received (which is unique) was in the table, and then it is written to the table if it does not already exist.

Playing with spaces indicates that an exception appears for the last URL on line 352.

I have already tried modifying the input on another line to see if this works by changing the code to use Python string operations (horror!) And having a drink. Nothing is working yet.

+4
2

INSERT WHERE.

UNIQUE ( , ) providerLink, INSERT IGNORE:

INSERT OR IGNORE INTO someSQLtable(providername, providerlink)
    VALUES ('somestring', 'http://www.someurl.com/stuff/')

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/')
+3

, sqllite, where . - :

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/') 

( (1)), T , , - Oracles dual DB2 sysibm.sysdummy1

0

Source: https://habr.com/ru/post/1543515/


All Articles