Team1-TU-tech / semi_final

0 stars 0 forks source link

pymysql.err.Error: Already closed #6

Open hamsunwoo opened 2 weeks ago

hamsunwoo commented 2 weeks ago
try:
            # 연결이 열려 있는지 확인하고, 닫혀 있으면 재연결
            if not conn.open:
                print("Database connection is closed. Reconnecting...")
                conn = get_conn()
                cursor = conn.cursor()

            cursor.execute(
                '''
                INSERT INTO ticket_data (event_name, price, date, location, available_tickets)
                VALUES (%s, %s, %s, %s, %s)
                ''',
                (ticket_data['event_name'], ticket_data['price'], ticket_data['date'], ticket_data['location'], ticket_data['available_tickets'])
            )
            conn.commit()  # 각 메시지 처리 후 커밋하여 DB에 반영
            print("Data saved to database.")
        except Exception as e:
            print(f"Error occurred while inserting data: {e}")
            conn.rollback()  # 오류 시 롤백하여 데이터 정합성 유지