batchupdate.py
changeset 33 bd0beda49bcb
parent 32 d59c473c9ad7
child 83 515fadd3d286
--- a/batchupdate.py	Fri Mar 23 14:54:04 2012 +0100
+++ b/batchupdate.py	Wed Mar 28 17:25:18 2012 +0200
@@ -4,6 +4,7 @@
 
 from pgtoolkit import toolbase
 from pgtoolkit.highlight import highlight
+from pgtoolkit.pgmanager import OperationalError
 
 
 class BatchUpdateTool(toolbase.SimpleTool):
@@ -15,16 +16,19 @@
 
     def main(self):
         # connect DB
-        with self.pgm.cursor('target') as curs:
-            rowcount = 1
-            while rowcount > 0:
-                print('query:', self.args.query)
-                curs.execute(self.args.query, [])
-                rowcount = curs.rowcount
-                print('updated', rowcount)
-                curs.connection.commit()
-                print('sleep %s seconds' % self.args.sleep)
-                time.sleep(self.args.sleep)
+        rowcount = 1
+        while rowcount > 0:
+            print('query:', self.args.query)
+            with self.pgm.cursor('target') as curs:
+                try:
+                    curs.execute(self.args.query, [])
+                    rowcount = curs.rowcount
+                    print('updated', rowcount)
+                    curs.connection.commit()
+                except (OperationalError, SystemError) as e:
+                    print('Error:', str(e))
+            print('sleep %s seconds' % self.args.sleep)
+            time.sleep(self.args.sleep)
 
 
 tool = BatchUpdateTool()