import tkinter as tk from tkinter import messagebox import mysql.connector def connect_to_database(): try: # 连接数据库 connection = mysql.connector.connect( host="127.0.0.1", user="root", password="020425", database="douban" ) cursor = connection.cursor() messagebox.showinfo("Success", "Connected to MySQL database successfully") return connection, cursor except Exception as e: messagebox.showerror("Error", f"Error connecting to MySQL database: {e}") root = tk.Tk() root.title("MySQL Database Example") # 连接数据库 connection, cursor = connect_to_database() root.mainloop() # 关闭数据库连接 connection.close()