import mysql.connector import os from db import DB_CONFIG def initialize_database(): try: # Connect to MySQL (without specific database) conn = mysql.connector.connect( host=DB_CONFIG["host"], user=DB_CONFIG["user"], password=DB_CONFIG["password"], port=DB_CONFIG["port"] ) cursor = conn.cursor() # Read and execute schema.sql schema_path = os.path.join(os.path.dirname(__file__), "schema.sql") with open(schema_path, "r", encoding="utf-8") as f: sql_commands = f.read().split(";") for command in sql_commands: if command.strip(): try: cursor.execute(command) except mysql.connector.Error as e: # Ignore table already exists errors or database creation if it exists if e.errno == 1007: # Database exists continue print(f"Executing command error: {e}") conn.commit() print("Database initialized/updated successfully") except mysql.connector.Error as err: print(f"Error connecting to MySQL: {err}") finally: if 'conn' in locals() and conn.is_connected(): cursor.close() conn.close() if __name__ == "__main__": initialize_database()