瀏覽代碼

Move commit to commit less often

Foppe Hemminga 6 年之前
父節點
當前提交
3a2e346c52
共有 1 個文件被更改,包括 14 次插入11 次删除
  1. 14 11
      main.py

+ 14 - 11
main.py

@@ -48,7 +48,7 @@ if __name__ == '__main__':
             continue
         # retrieve previous stored scratching for this venue / day
         query = "SELECT * FROM horses WHERE venue = %s AND race_date = %s;"
-
+        db.commit()
         cur1 = db.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
         cur1.execute(query, (race_day.name, race_day.date))
         db_data_query1 = cur1.fetchall()
@@ -58,6 +58,7 @@ if __name__ == '__main__':
         cur1.close()
 
         # compare retrieved scratchings with new data
+        scratchings_for_database = []
         for scratching in scratchings:
             found_in_database = False
             # if db_data is None:
@@ -91,7 +92,7 @@ if __name__ == '__main__':
                         found_in_database = True
                 except AttributeError as ae:
                     print(ae)
-                    print(' 89. row: ', end='')
+                    print(' 94. row: ', end='')
                     print(row)
                     pprint(db_data_query1)
                     continue
@@ -133,16 +134,18 @@ if __name__ == '__main__':
                 if broadcast:
                     view.broadcast(message)
                 # store new scratching
-                query = """INSERT INTO horses(venue, race_date, race, horse) 
-                    VALUES(%s, %s, %s, %s)
-                    ON CONFLICT(venue, race_date, race, horse) DO NOTHING;"""
+                scratchings_for_database.append((scratching.venue, scratching.date,
+                                                 scratching.race, scratching.horse))
+        query = """INSERT INTO horses(venue, race_date, race, horse) 
+            VALUES(%s, %s, %s, %s)
+            ON CONFLICT(venue, race_date, race, horse) DO NOTHING;"""
+
+        cur3 = db.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
+        for database_entry in scratchings_for_database:
+            cur3.execute(query, database_entry)
+            print('Stored: {}'.format(database_entry))
+        cur3.close()
 
-                cur3 = db.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
-                cur3.execute(query, (scratching.venue, scratching.date,
-                                     scratching.race, scratching.horse))
-                cur3.close()
-                print('Stored: {}'.format(scratching))
-                time.sleep(0.5)
         db.commit()
     db.close()