sqlite if exist update else insert in python
I am trying to update Count=Count+1 if src_ip already exists, else insert into table. I was able to do it with UPSERT successfully: INSERT INTO result('min(_time)', src_ip) VALUES(?,?)ON CONFLICT(src_ip) DO UPDATE SET Count=Count+1; I am looking for another way to do this without using UPSERT. I tried this : # Import csv to SQLite with open('result5.csv', 'r') as f: dr = csv.DictReader(f) for i in dr: c.execute("UPDATE result SET Count = Count + 1 WHERE src_ip = ?", (i['src_ip'],)) if c.rowcount == 0: c.execute("INSERT INTO result ('min(_time)', src_ip, Count) VALUES (?,?,1)", (i['min(_time)']
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.