Using `executemany` to update entries in an existing SQLite3 database (using Python sqlite3)

后端 未结 2 1552
梦毁少年i
梦毁少年i 2021-02-14 20:00

I know that executemany can be used to conveniently add new entries to a database; useful to reduce the Python method-call overheads compared to single execut

相关标签:
2条回答
  • 2021-02-14 20:34

    You need to pass a sequence of sequences ([[cont,id], [cont,id], [cont,id], ...], not [cont, cont, cont, ...], [id, id, id, ..]):

    for path in paths:
        params = []
        for data in some_computation(path):
            if len(data_ids) >= CHUNKSIZE:
                c.executemany("UPDATE TABLENAME SET cont=? WHERE id=?", params)
                cnx.commit()
                params = []
            params.append([data[1], data[0]])
        if params:
            c.executemany("UPDATE TABLENAME SET cont=? WHERE id=?", params)
        cnx.commit()
    
    0 讨论(0)
  • 2021-02-14 20:37

    what you have is perfect except that you should use zip(conts,ids) where conts and ids are lists. This does the rearrangement automatically for you.

    0 讨论(0)
提交回复
热议问题