简体   繁体   中英

sqlite problem : sqlite3.OperationalError: near “where”: syntax error

I'm trying to insert data from my crawler into my db without duplicate.

However,

sqlite3.OperationalError: near "where": syntax error

c.execute('insert into stocks(stocknum) values (?) where not exists(select * from stocks)',(stock_num,))

above is my code to insert. I'm sure there is a problem near "where" but I couldn't debug it.

If I followed you correctl, you want to insert values that do not yet exist in column stocknum .

Your immediate problem is that your query is not valid SQLite syntax. You cannot use values() with a where clause, you would need to select instead:

insert into stocks(stocknum) 
select ? 
where not exists(select * from stocks)

Now this is valid SQL, but does not do what you want. It inserts only if stocks is entirely empty. You would need to correlate the subquery with the outer query (which requires passing the parameter twice, or using a subquery):

insert into stocks(stocknum) 
select ?
where not exists(select 1 from stocks where stocknum = ?)

Finally: if you are running SQLite 3.24 or higher, this is simpler achieved tiwh the on conflict clause . For this to work, you need a unique (or primary key) constraint on column stocknum . Then you can do:

insert into stocks(stocknum) 
values (?)
on conflict(stocknum) do nothing

Try adding IGNORE :

c.execute('INSERT IGNORE INTO stocks(stocknum) values (?) where not exists(select * from stocks)',(stock_num,))

I think is %s not ? .You can find documentation here

query = 'insert into stocks(stocknum) values (%s) where not exists(select * from stocks)'
args = (stock_num)

c.execute(query,args)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM