[Tutor] sqlite3 COMMIT directive

Alex Kleider akleider at sonic.net
Tue Oct 1 05:16:49 CEST 2013


I'm just beginning to enter into the field of database and SQLite in 
particular.

Python 2.7 on Ubuntu

The problem code is a class method defined as follows:


     def insert(self, DataBase=DataBase, Table=Table):
         """
         Insert instance of ClassMate into <DataBase> <Table>.
         """
         con = sqlite3.connect(DataBase)
         with con:
             cur.execute("""INSERT INTO %s VALUES
             (NULL, '%s', '%s', '%s', '%s', '%s', '%s');""" % \
             (Table, self.first, self.last, self.partner,
             self.address, self.phone, self.email, )      )
             try:
                 cur.execute("COMMIT;")
             except:
                 pass

And the problem = \
"""
If the 'cur.execute("COMMIT;")' statement is left by itself, outside of 
a
"try/except" clause, I get an error with the following trace back:
"
Traceback (most recent call last):
   File "./uwomeds68.py", line 119, in <module>
     go_on.insert()
   File "./uwomeds68.py", line 64, in insert
     cur.execute("COMMIT;")
sqlite3.OperationalError: cannot commit - no transaction is active
"

Without the COMMIT, there's no error but the data doesn't get added to 
the data base.
"""

I've been reading about the COMMIT directive but apart from its 
relationship with ROLLBACK, I haven't been able to figure out how it is 
relevant to my problem here.

Any advice/explanation would of course be appreciated.

Alex Kleider


More information about the Tutor mailing list