首先引用tkinter和pymysql库;
import tkinter
import pymysql
构造一个格式化时间戳的类
#遇到日期特殊处理
class ComplexEncoder(json.JSONEncoder):
def default(self,obj):
if isinstance(obj,datetime):
return obj.strftime('%Y-%m-%d %H:%M:%S')
elif isinstance(obj,date):
return obj.strftime('%Y-%m-%d')
else:
return json.JSONEncoder.default(self,obj)
定义一个函数,实现sql查询的功能,使用entry和combobox获取用户的输入,使用open函数将查询结果导出并存储到D盘;
#定义查询函数
def interface_table_cx_sql():
try:
db = pymysql.connect(host=ip_down_menu.get(),port=int(port_down_menu.get()),user=user_down_menu.get(),password=pw_down_menu.get(),database=db_down_menu.get(), charset='utf8' )
tkinter.messagebox.showinfo('消息框', '数据库连接成功', parent=win)
except:
tkinter.messagebox.showerror('消息框', 'error 数据库连接失败', parent=win)
cursor = db.cursor() #创建一个游标
sql = "select * from " + interface_table_down_menu.get()
try

本文介绍如何使用Tkinter和pymysql库,通过用户输入实现数据库查询,并将结果格式化为JSON并保存到D盘。包括时间戳处理和错误处理的代码示例。
最低0.47元/天 解锁文章
1016

被折叠的 条评论
为什么被折叠?



