sqlite如果存在,请在python中更新else insert

2024-04-24 11:33:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试更新Count=Count+1,如果src_ip已经存在,否则插入到表中。在

我用UPSERT成功地做到了:

INSERT INTO result('min(_time)', src_ip) VALUES(?,?)ON CONFLICT(src_ip) DO UPDATE SET Count=Count+1;

我正在寻找另一种不使用UPSERT的方法。我试了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']))

脚本运行正常。但我也得不到我想要的结果。它没有任何可悲的回报。我不知道我做错了什么,为什么我不能UPDATE或者{}?在

谢谢。在


Tags: csvipsrcexecutetimecountupdateresult