0

So, I have this script for python using sqlite3:

cursor.execute("""if exists (select * from items where defindex=:def)    
update items set imgurl=:img,name=:name where defindex=:def    
else 
insert into items values(:img,:def,:name)""",{"def":item['defindex'],"img":    item['image_url_large'],"name": item['name']})

Why would OperationalError: near "if": syntax error appear? I tried searching, but couldn't find anything.

RomaValcer
  • 2,786
  • 4
  • 19
  • 29
  • afaik you need stored procedures for a IF EXISTS UPDATE ELSE INSERT contstruct in SQL. you could however look into "INSERT OR REPLACE" http://stackoverflow.com/questions/3634984/insert-if-not-exists-else-update – Gryphius Jan 08 '14 at 06:58

1 Answers1

0

Thanks to Gryphius, I replaced my if with insert or replace. It worked for me pretty much.

cursor.execute("insert or replace into items values(:id,:img,:def,:name)",{"id":itemid,"def":item['defindex'],"img": item['image_url_large'],"name": item['name']})
RomaValcer
  • 2,786
  • 4
  • 19
  • 29