Re: Does .commit() ensure the .execute()'s and .executemany()'s called before are run atomically?
David Raymond <[email protected]>
| Newsgroups | gmane.comp.db.sqlite.general |
|---|---|
| Message-ID | <VI1PR07MB6029A18DBDB63F2B2310C62E87040@VI1PR07MB6029.eurprd07.prod.outlook.com> |
-----Original Message----- From: sqlite-users <[email protected]> On Behalf Of Peng Yu Sent: Thursday, January 30, 2020 5:16 AM To: SQLite mailing list <[email protected]> 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 [email protected] http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users