Nifty database framework

Database framework for sql queries, execute and rollbacks

In database management, it’s useful to define atomic blocks. And let’s say if something fails within this block, you can always revert to the initial state before any of the transaction happens.

Below is a nifty framework,

  • Execute the query while not changing the state in the database.
  • If there’re any errors along within the atomic block, you capture the error under exception area. And rollback to the initial state.
  • If everything goes well, commit the cached queries to the database.
Exceptions = []

for i in range(3):

    try:
        i/0
        print('cursor.Execute()')
    except Exception as e:
        Exceptions.append({
        'id': i,
        'error': str(e)        
        })
        
if len(Exceptions) > 0:
    print(Exceptions)
    print('cursor.rollback()')
    
else:
    print('cursor.commit() to database')

Related

comments powered by Disqus