logo       

Re: Emulating a sequence: msg#00028

python.db.pysqlite.user

Subject: Re: Emulating a sequence

David Pratt wrote:
> [...]
> I figure excutescript should execute both statements in a single
> transaction so it will be as safe way to go. The value will always be
> correct without worrying that another connection picked off the next
> value before another committed it. [...]

As I said in the other message, transactions "just work" - with and
without executescript().

- pysqlite (as mandated by the DB-API 2.0) transparently opens
transactions for you once you use a DML command (INSERT, UPDATE, DELETE,
REPLACE). It also transparently COMMITs if you use a DDL command
(CREATE, DROP, ...).

- Then, just use as many DML commands as you want
- If you're in a consistent state, use .commit() on the connection object.

- If you want to roll back your changes, use .rollback() on the
connection object.

A sequence like this is quite common for using the DB-API:

class DataAccessLayer:
...

def some_action(self):
try:
cur = self.con.cursor()
cur.execute(...)
cur.execute(...)
cur.execute(...)

self.con.commit()
except:
self.con.rollback()

-- Gerhard


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise