Trying to fix Lost connection to MySQL server during query
This commit is contained in:
50
bot.py
50
bot.py
@@ -105,20 +105,25 @@ db_cursor = db_connection.cursor()
|
||||
def close_database_connection(connection):
|
||||
connection.close()
|
||||
|
||||
def insert_user_data(user_id, guild_id, permission, points, ban, askmultus, filter_value, chat_history, nickname, profile_picture, join_date, xp=0, level=1):
|
||||
def insert_user_data(user_id, guild_id, permission, points, ban, askmultus, filter_value, chat_history, xp=0, level=1, nickname="", profile_picture="", join_date=None, leave_date=None):
|
||||
insert_query = """
|
||||
INSERT INTO user_data (user_id, guild_id, permission, points, ban, askmultus, filter_value, rank, chat_history, nickname, profile_picture, join_date, xp, level, leave_date)
|
||||
INSERT INTO user_data (user_id, guild_id, permission, points, ban, askmultus, filter_value, rank, chat_history, xp, level, nickname, profile_picture, join_date, leave_date)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
||||
"""
|
||||
serialized_chat_history = json.dumps(chat_history)
|
||||
data = (user_id, guild_id, permission, points, ban, askmultus, filter_value, 0, serialized_chat_history, nickname, profile_picture, join_date, xp, level, None)
|
||||
try:
|
||||
db_cursor.execute(insert_query, data)
|
||||
data = (user_id, guild_id, permission, points, ban, askmultus, filter_value, 0, serialized_chat_history, xp, level, nickname, profile_picture, join_date, leave_date)
|
||||
|
||||
def execute_insert():
|
||||
cursor = get_database_cursor()
|
||||
cursor.execute(insert_query, data)
|
||||
db_connection.commit()
|
||||
|
||||
try:
|
||||
retry_query(execute_insert)
|
||||
print("User data inserted successfully.")
|
||||
except Exception as e:
|
||||
print(f"Error inserting user data: {e}")
|
||||
db_connection.rollback()
|
||||
print(f"Error inserting user data after retries: {e}")
|
||||
|
||||
|
||||
def update_user_data(user_id, guild_id, field, value):
|
||||
global db_connection, db_cursor # global-Deklaration muss vor dem Zugriff erfolgen
|
||||
@@ -149,13 +154,42 @@ def update_user_data(user_id, guild_id, field, value):
|
||||
|
||||
|
||||
def connect_to_database():
|
||||
return mysql.connector.connect(
|
||||
connection = mysql.connector.connect(
|
||||
host=DB_HOST,
|
||||
port=DB_PORT,
|
||||
user=DB_USER,
|
||||
password=DB_PASSWORD,
|
||||
database=DB_DATABASE
|
||||
)
|
||||
connection.autocommit = True # Automatisches Commit für stabilere Abfragen
|
||||
return connection
|
||||
|
||||
def retry_query(func, *args, retries=3, delay=5):
|
||||
for _ in range(retries):
|
||||
try:
|
||||
return func(*args)
|
||||
except mysql.connector.Error as err:
|
||||
print(f"Retrying due to error: {err}")
|
||||
time.sleep(delay)
|
||||
raise RuntimeError("Max retries exceeded")
|
||||
|
||||
def get_database_cursor():
|
||||
if not db_connection.is_connected():
|
||||
db_connection.reconnect(attempts=3, delay=5)
|
||||
return db_connection.cursor()
|
||||
|
||||
pool = mysql.connector.pooling.MySQLConnectionPool(
|
||||
pool_name="mypool",
|
||||
pool_size=10,
|
||||
host=DB_HOST,
|
||||
port=DB_PORT,
|
||||
user=DB_USER,
|
||||
password=DB_PASSWORD,
|
||||
database=DB_DATABASE
|
||||
)
|
||||
|
||||
def connect_to_database():
|
||||
return pool.get_connection()
|
||||
|
||||
def close_database_connection(connection):
|
||||
connection.close()
|
||||
|
||||
Reference in New Issue
Block a user