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)'], i['src_ip']))
The script works with no errors. But I am not getting the result I want either. It does not return anything sadly. I am not sure what I am doing wrong here, why can't I UPDATE or INSERT?
Thank you.
$src_ipWHERE CLIENT_IP = ?', ("192.168.10.111",)the issue with my script is I am trying to prevent duplicates into database. I want to updateCount=Count+1based onWHERE src_ip LIKE src_iporWHERE src_ip=?", ('src_ip',)This is where I am stuck. I am trying to avoid duplicatesrc_ipgetting into my database, insteadcount=count+1if duplicate is found so I know it has been entered twice and not wasting rowsWHERE src_ip LIKE src_ipis faulty, read sql-statement-with-like-from-variable, and it does NOT help to avoid duplicates. Edit your Question and explain in detail why do you want to useLIKE.drtwice,to_ipis already a list, usingexecutemanybut want to use a condition) - It's to broad for SO, setup a GitHub Gist