From 4bf39cc50a6d44e987831da985f64393a53adcba Mon Sep 17 00:00:00 2001 From: dqs2956213868 <2956213868@qq.com> Date: Wed, 29 May 2024 16:10:15 +0800 Subject: [PATCH] commit_0529 --- PythonRequest01/Tkinter/MySql.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 PythonRequest01/Tkinter/MySql.py 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()