Sfoglia il codice sorgente

Attempt to fix weird row unpack error

Foppe Hemminga 6 anni fa
parent
commit
ccbc61be3b
1 ha cambiato i file con 14 aggiunte e 8 eliminazioni
  1. 14 8
      main.py

+ 14 - 8
main.py

@@ -36,7 +36,7 @@ if __name__ == '__main__':
     interim = time.time()
     # print('interim 2 {}'.format(interim - start))
     # pprint(race_days)
-    cursor = db.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
+
     for race_day in race_days:
         if not raw_data_dict or not race_day:
             # raw_data_dict may be empty when there is no data available (yet)
@@ -49,11 +49,13 @@ if __name__ == '__main__':
         # retrieve previous stored scratching for this venue / day
         query = "SELECT * FROM horses WHERE venue = %s AND race_date = %s;"
 
-        cursor.execute(query, (race_day.name, race_day.date))
-        db_data = cursor.fetchall()
+        cur1 = db.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
+        cur1.execute(query, (race_day.name, race_day.date))
+        db_data = cur1.fetchall()
         print(' 54. len(db_data): {}'.format(len(db_data)))
         print(' 55. type(db_data): {}'.format(type(db_data)))
         pprint(db_data)
+        cur1.close()
 
         # compare retrieved scratchings with new data
         for scratching in scratchings:
@@ -103,8 +105,10 @@ if __name__ == '__main__':
                         venue = %s AND
                         race = %s;
                 """
-                cursor.execute(query, (scratching.date, scratching.venue, scratching.race))
-                db_data = cursor.fetchone()
+                cur2 = db.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
+                cur2.execute(query, (scratching.date, scratching.venue, scratching.race))
+                db_data = cur2.fetchone()
+                cur2.close()
                 if db_data is None or len(db_data) == 0:
                     print('106. cursor.execute(query, (scratching.date, scratching.venue, scratching.race))')
                     print('107. cursor.execute({}, ({}, {}, {}))'.format(
@@ -132,12 +136,14 @@ if __name__ == '__main__':
                 query = """INSERT INTO horses(venue, race_date, race, horse) 
                     VALUES(%s, %s, %s, %s)
                     ON CONFLICT(venue, race_date, race, horse) DO NOTHING;"""
-                cursor.execute(query, (scratching.venue, scratching.date,
-                                       scratching.race, scratching.horse))
+
+                cur3 = db.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
+                cur3.execute(query, (scratching.venue, scratching.date,
+                                     scratching.race, scratching.horse))
                 db.commit()
+                cur3.close()
                 print('Stored: {}'.format(scratching))
                 time.sleep(0.5)
-    cursor.close()
     db.close()
 
     interim = time.time()