-----Original Message-----
From: sqlite-users <sqlite-users-boun...@mailinglists.sqlite.org> On Behalf Of 
Peng Yu
Sent: Thursday, January 30, 2020 5:16 AM
To: SQLite mailing list <sqlite-users@mailinglists.sqlite.org>
Subject: Re: [sqlite] Does .commit() ensure the .execute()'s and 
.executemany()'s called before are run atomically?

Could you show a python example on how to make multiple entries either
being all inserted (each entry done by an insert statement) or none on any
error (e.g. ctrl-c)? I also want want to make sure no two python processes
simultaneously editing these entries.

I am not sure I fully understand how to implement this correctly.



Using the manual transactions (isolation_level = None) it might look something 
along the lines of this:
<Braces for impact of comments on my style>


import sqlite3
import contextlib

conn = sqlite3.connect(myFile, isolation_level = None)
try:
    with contextlib.closing(conn.cursor()) as cur:
        #stuff
        cur.execute("begin;")
        try:
            for thing in otherThing:
                cur.execute("insert into myTable values (?,?,?);", thing)
        except:
            conn.rollback()
            print("Error message here")
            return someCode #or re-raise the exception
        else:
            conn.commit()
finally:
    conn.close()
_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to