class Cursor(BaseCursor): driver = "sqlite" # sqlite is smart - this is only a noop def binary(self, s): return s # capture and translate exceptions def _tryExecute(self, func, *params, **kw): try: ret = func(*params, **kw) except sqlite3.ProgrammingError, e: if ((e.args[0].startswith("column") and e.args[0].endswith("not unique")) or e.args[0].startswith("UNIQUE constraint failed")): raise sqlerrors.ColumnNotUnique(e) elif e.args[0] == 'attempt to write a readonly database': raise sqlerrors.ReadOnlyDatabase(str(e)) raise sqlerrors.CursorError(e.args[0], e) except sqlite3.DatabaseError, e: if e.args[0].startswith('duplicate column name:'): raise sqlerrors.DuplicateColumnName(str(e)) if e.args[0].startswith('database is locked'): raise sqlerrors.DatabaseLocked(str(e)) if e.args[0].startswith("no such table"): raise sqlerrors.InvalidTable(str(e)) raise sqlerrors.CursorError(e.args[0], e)
def connect(self, **kwargs): assert (self.database) kwargs.setdefault("timeout", self.TIMEOUT) #kwargs.setdefault("command_logfile", open("/tmp/sqlite.log", "a")) #kwargs.setdefault("lockJournal", True) try: self.dbh = sqlite3.connect(self.database, **kwargs) except sqlite3.InternalError, e: if str(e) == 'database is locked': raise sqlerrors.DatabaseLocked(e) raise
# sqlite is more peculiar when it comes to firing off transactions def transaction(self, name=None): assert (self.dbh) cu = self.cursor() if self.dbh.inTransaction: return cu try: self.dbh._begin() except sqlite3.ProgrammingError, e: if str(e) == 'attempt to write a readonly database': raise sqlerrors.ReadOnlyDatabase(str(e)) raise except sqlite3.DatabaseError, e: if e.args[0].startswith('database is locked'): raise sqlerrors.DatabaseLocked(str(e)) raise return cu def rollback(self, name=None): assert not name # When using pysqlite3, which contains mixed python and C code, a # signal arriving during a commit or rollback results in the sqlite # library committing but the pysqlite3 transaction state not being # updated. Try to fix this when the subsequent rollback happens. # With the Python stdlib bindings everything is C, including the # transaction flag, so this does not happen. The signal exception still # gets raised after the db has already finished committing but the # connection works normally afterwards. try: