diff --git a/PythonRequest01/Tkinter/MySql.py b/PythonRequest01/Tkinter/MySql.py new file mode 100644 index 0000000..2c631df --- /dev/null +++ b/PythonRequest01/Tkinter/MySql.py @@ -0,0 +1,30 @@ +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()