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

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

时间:2024-08-09 10:26:07浏览次数:7  
标签:连通性 python text column 测试通过 grid tk root row

免费分享刚刚写的一个小程序,测试通过没问题,解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()

运行结果如下:

关注公众号,多多交流!

标签:连通性,python,text,column,测试通过,grid,tk,root,row
From: https://blog.csdn.net/m0_38111284/article/details/141054947

相关文章

  • Python面试宝典第30题:找出第K大元素
    题目        给定一个整数数组nums,请找出数组中第K大的数,保证答案存在。其中,1<=K<=nums数组长度。        示例1:输入:nums=[3,2,1,5,6,4],K=2输出:5        示例2:输入:nums=[50,23,66,18,72],K=3输出:50快速选择算法......
  • 使用Python和Flask框架实现简单的RESTful API
    目录环境准备创建Flask应用运行Flask应用测试API注意事项在当今的Web开发领域,RESTfulAPI因其简洁性和高效性而备受欢迎。本文将引导你使用Python的Flask框架来创建一个简单的RESTfulAPI,用于增删改查(CRUD)用户信息。环境准备在开始之前,请确保你的Python环境中已经安......
  • nodejs语言,MySQL数据库;springboot的个性化资讯推荐系统66257(免费领源码)计算机毕业设计
    摘 要随着科学技术的飞速发展,社会的方方面面、各行各业都在努力与现代的先进技术接轨,通过科技手段来提高自身的优势,个性化资讯推荐系统当然也不能排除在外。个性化资讯推荐系统是以实际运用为开发背景,运用软件工程原理和开发方法,采用springboot技术构建的一个管理系统。整......
  • c#语言,SQL server数据库;基于Web的社区人员管理系统的设计与实现36303(免费领源码)计算机
    目 录摘要1绪论1.1慨述1.2课题意义1.3B/S体系结构介绍1.4ASP.NET框架介绍2 社区人员管理系统分析2.1可行性分析2.2系统流程分析2.2.1数据增加流程2.2.2数据修改流程52.2.3数据删除流程52.3系统功能分析62.3.1功能性分析62.3.2非功能性......
  • Python多种接口请求方式示例
    发送JSON数据如果你需要发送JSON数据,可以使用json参数。这会自动设置Content-Type为application/json。importrequestsimportjsonurl='http://example.com/api/endpoint'data={"key":"value","another_key":"another_value"......
  • 【优秀python毕设案例】基于python django的新媒体网络舆情数据爬取与分析
    摘   要如今在互联网时代下,微博成为了一种新的流行社交形式,是体现网络舆情的媒介之一。现如今微博舆论多带有虚假不实、恶意造谣等负面舆论,为了营造更好的网络环境,本设计提出了基于新媒体的网络舆情数据爬取与分析,主要对微博热点话题进行处理。本设计首先以Python为环......
  • 在 Rust 中嵌入 Python 来调用外部 Python 库
    我正在尝试学习如何将Python嵌入到Rust应用程序中。出于学习目的,我想创建一个运行永远循环的Rust脚本/应用程序。该循环会休眠设定的时间间隔,醒来后,它使用Pythonrequests库从互联网时间服务器获取当前时间。虽然这不是一个实际应用程序,但我的目标是了解如何从Rust调用......
  • 如何从我的 Python 应用程序更新我的 Facebook Business 令牌?
    我有一个使用FacebookBusiness库的Python应用程序。因此,我需要使用Facebook提供的令牌来访问我的见解并操纵它们。但是,这个令牌有一个很长的到期日期,但我想知道是否有办法自动更新这个令牌在我的应用程序中,这样它就不会停止运行。当然可以!你可以使用Facebook提......
  • 哪种编程语言更适合学习数据结构和算法:C++、Java 还是 Python?
    作为一名工程专业的学生,​​我正在尝试决定使用哪种编程语言来学习数据结构和算法(DSA)。我正在考虑C++,它提供高性能和强大的标准模板库,但对于初学者来说可能很复杂。Java具有强大的语法和内置集合,使DSA概念更容易掌握,尽管我不确定它与C++相比的性能。Python以其简单性和......
  • 同时运行多个异步阻塞函数 - Python
    我是Python和协程的新手,我正在尝试利用Python的asyncio库来并行处理阻塞函数。我正在使用python3.8.6。我有一个阻塞函数,它从数组输入中接收不同的输入,我需要每个输入的阻塞函数同时运行。我已经尝试过,但它们似乎仍然按顺序运行:asyncdefmain():tasks=[asyncio......