# Collection of code snippets by Arne Vajhøj 
# (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import pymysql

from DB import *

class StdTx(DB):
    def get_connection(self):
        return pymysql.connect(host='localhost',user='root',password='hemmeligt',db='Test')
    def test(self):
        for i in range(10):
            con = self.get_connection()
            c = con.cursor()
            try:
                c.execute("UPDATE tblsrc SET val = val - 1 WHERE id = 1")
                if i % 2 != 0:
                    raise Problem()
                c.execute("UPDATE tbltrg SET val = val + 1 WHERE id = 1")
                con.commit()
            except Problem:
                con.rollback()
                print('Problem')
            c.close()
            con.commit()

demo(StdTx())
