From 7d741e2df112968f3eafc88f57622c2f0d73e11b Mon Sep 17 00:00:00 2001 From: SimolZimol <70102430+SimolZimol@users.noreply.github.com> Date: Sat, 14 Sep 2024 19:27:51 +0200 Subject: [PATCH] modified: bot.py --- bot.py | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/bot.py b/bot.py index 4ae98c2..122941d 100644 --- a/bot.py +++ b/bot.py @@ -181,17 +181,30 @@ def insert_user_data(user_id, permission, points, ban, askmultus, filter_value, def update_user_data(user_id, field, value): - update_query = f"UPDATE user_data SET {field} = %s WHERE user_id = %s" - - # Überprüfen, ob das Feld 'chat_history' aktualisiert wird - if field == 'chat_history': - # Serialize the chat history list to a JSON string - serialized_chat_history = json.dumps(value) - db_cursor.execute(update_query, (serialized_chat_history, user_id)) - else: - db_cursor.execute(update_query, (value, user_id)) - - db_connection.commit() + try: + update_query = f"UPDATE user_data SET {field} = %s WHERE user_id = %s" + + # Überprüfen, ob das Feld 'chat_history' aktualisiert wird + if field == 'chat_history': + # Serialize the chat history list to a JSON string + serialized_chat_history = json.dumps(value) + db_cursor.execute(update_query, (serialized_chat_history, user_id)) + else: + db_cursor.execute(update_query, (value, user_id)) + + db_connection.commit() + + except mysql.connector.Error as err: + logger.error(f"Database error: {err}") + if db_connection.is_connected(): + db_cursor.close() + db_connection.close() + # Verbindung neu aufbauen + global db_connection, db_cursor + db_connection = connect_to_database() + db_cursor = db_connection.cursor() + # Wiederhole die Abfrage nach dem erneuten Verbinden + update_user_data(user_id, field, value) def connect_to_database(): return mysql.connector.connect(