0

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.

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

Comments

1

Try adding IGNORE:

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

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.