使用python做页面,测试数据库连通性!免费分享!测试通过~

免费分享刚刚写的一个小程序,测试通过没问题,解BUG也就花了半小时吧

有更好的方法欢迎评论区推给我谢谢。

import tkinter as tk
from tkinter import messagebox
import pymysql

def get_db_info(db_source):
    if db_source=='database1':
        hostname = e1.get()
        username = e2.get()
        password = e3.get()
        database = e4.get()
        port = e5.get()
    elif db_source=='database2':
        hostname = e11.get()
        username = e22.get()
        password = e33.get()
        database = e44.get()
        port = e55.get()
    return hostname, username, password, database, port

def test_connection(db_source):
    try:
        hostname, username, password, database, port = get_db_info(db_source)
        connection = pymysql.connect(host=hostname, user=username, password=password, db=database, port=int(port))
        with connection.cursor() as cursor:
            cursor.execute("SELECT VERSION()")
            result = cursor.fetchone()
            messagebox.showinfo("数据库版本", f"数据库版本: {result}")
            connection.close()
    except pymysql.MySQLError as e:
        messagebox.showerror("连接错误", f"无法连接到数据库: {e}")


root = tk.Tk()
root.title("数据库连接测试")
tk.Label(root, text="源端数据库信息").grid(row=0, column=0)
tk.Label(root, text="主机名:      ").grid(row=1, column=0)
tk.Label(root, text="用户名:      ").grid(row=2, column=0)
tk.Label(root, text="密码:       ").grid(row=3, column=0)
tk.Label(root, text="数据库名:    ").grid(row=4, column=0)
tk.Label(root, text="端口:       ").grid(row=5, column=0)

e1 = tk.Entry(root)
e2 = tk.Entry(root)
e3 = tk.Entry(root, show='*')
e4 = tk.Entry(root)
e5 = tk.Entry(root)

e1.grid(row=1, column=1)
e2.grid(row=2, column=1)
e3.grid(row=3, column=1)
e4.grid(row=4, column=1)
e5.grid(row=5, column=1)

button = tk.Button(root, text="测试连接", command=test_connection('database1'))
button.grid(row=6, column=0, sticky=tk.W, pady=10)

tk.Label(root, text="目标端数据库信息").grid(row=7, column=0)
tk.Label(root, text="主机名:      ").grid(row=8, column=0)
tk.Label(root, text="用户名:      ").grid(row=9, column=0)
tk.Label(root, text="密码:       ").grid(row=10, column=0)
tk.Label(root, text="数据库名:    ").grid(row=11, column=0)
tk.Label(root, text="端口:       ").grid(row=12, column=0)

e11 = tk.Entry(root)
e22 = tk.Entry(root)
e33 = tk.Entry(root, show='*')
e44 = tk.Entry(root)
e55 = tk.Entry(root)

e11.grid(row=8, column=1)
e22.grid(row=9, column=1)
e33.grid(row=10, column=1)
e44.grid(row=11, column=1)
e55.grid(row=12, column=1)

button = tk.Button(root, text="测试连接", command=test_connection('database2'))
button.grid(row=13, column=0, sticky=tk.W, pady=10)

root.mainloop()

运行结果如下:

关注公众号,多多交流!

  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_38111284

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值