I am trying to commit entries to a MySQL
database using SQLAlchemy
in Python
. The function below was meant to allow me to commit a batch of entries, even if one or two of them contained errors that the database would not accept. However, when it gets to an entry that it cannot commit it does not skip it and go to the next one, it tries to commit the same entry over and over and over again, getting the same error message for that entry a thousand times. Why doesn't the trycatch in my function work?
def commitentry(database, enginetext, verbose = False):
"""
Takes a database object and text string that defines the SQL
engine and adds all entries in the database list to the SQL
database.
"""
engine = create_engine(enginetext)
Session = sessionmaker()
Session.configure(bind=engine)
session = Session()
counter = 0
for entry in database:
try:
session.add(entry)
session.commit()
except Exception, e:
print("Commit Error")
if verbose:
print(e)
finally:
counter += 1
if verbose:
print(counter, counter/float(len(database)))
if verbose:
print("Entries saved!")
session.close()
This Session's transaction has been rolled back due to a previous exception during flush. To begin a new transaction with this Session, first issue Session.rollback(). Original exception was: (OperationalError) (1366, "Incorrect integer value: 'R' for column 'district' at row 1")...
with ... being all the rest of the info about the entry. Basically it's saying you can't put text in an integer column. – Michael 2 days agosession.rollback()
to the except block and now it works. It seems that a commit that fails leaves the failed object right where it is to fail again on the next commit. – Michael 2 days ago