本文整理自网络,侵删。
目录
- 一、准备工作:
- 二、代码:
- 三、界面
- 四、总结
一、准备工作:
1、安装mysql3.7,创建一个test数据库,创建student表,创建列:(列名看代码),创建几条数据
(以上工作直接用navicat for mysql工具完成)
二、代码:
import sys import tkinter as tk import mysql.connector as sql #--------------------查询函数--------------------------- def sql_connect(): ? ? listbox_show.delete(0,'end') #初始化清空显示区 ? ? m = listbox_name.curselection()[0] ? ? print('m=',m) ? ? list_name = listbox_name.get(m)[0] ? ? varlue = entry_varlue.get() ? ? print('list_name=',list_name) ? ? print('var=',varlue) ? ? while list_name == 'Student_ID': ? ? ? ? sql_sr = "select * from Student where student.student_id = " + "'" + varlue +"'" ? ? ? ? print(sql_sr) ? ? ? ? break ? ? while list_name == 'Name': ? ? ? ? sql_sr = "select * from Student where student.name = " + "'" + varlue +"'" ? ? ? ? print(sql_sr) ? ? ? ? break ? ? while list_name == 'Age': ? ? ? ? sql_sr = "select * from Student where student.age = " + "'" + varlue +"'" ? ? ? ? print(sql_sr) ? ? ? ? break ? ? while list_name == 'Sex': ? ? ? ? sql_sr = "select * from Student where student.sex = " + "'" + varlue +"'" ? ? ? ? print(sql_sr) ? ? ? ? break ? ? while list_name == 'Class': ? ? ? ? sql_sr = "select * from Student where student.class = " + "'" + varlue +"'" ? ? ? ? print(sql_sr) ? ? ? ? break ? ? mydb = sql.connect(host='localhost',user='root',passwd='123456',database='test') ? ? mycursor = mydb.cursor() ? ? mycursor.execute(sql_sr) ? ? myresult = mycursor.fetchall() ? ? for x in myresult: ? ? ? ? print (x) ? ? ? ? listbox_show.insert('end',x) #-------------------------------------------------------- #------------界面初始化时加载选项列表-------------------- sql_n = "SELECT COLUMN_NAME FROM information_schema.COLUMNS \ WHERE TABLE_SCHEMA = 'test' AND TABLE_NAME = 'student'" mydb = sql.connect(host='localhost',user='root',passwd='123456',database='test') mycursor = mydb.cursor() mycursor.execute(sql_n) myresult = mycursor.fetchall() #-------------------------------------------------------- #----------窗体部分-------------------------------------- entry_list_name = tk.Entry() entry_list_name.pack() listbox_name = tk.Listbox() listbox_name.pack() for i in myresult: ? ? listbox_name.insert('end',i) #加载选项列表 entry_varlue = tk.Entry() entry_varlue.pack() button_select = tk.Button(text = '查找',command = sql_connect) button_select.pack() listbox_show = tk.Listbox() listbox_show.pack() root.mainloop() #--------------------------------------------------------
三、界面
相关阅读 >>
数据库高并发请求,如何保证数据完整性?详解mysql/innodb的加锁
更多相关阅读请进入《mysql》频道 >>

数据库系统概念 第6版
机械工业出版社
本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。
转载请注明出处:木庄网络博客 » python+tkinter+mysql做简单数据库查询界面
标签:mysql
相关推荐
评论
管理员已关闭评论功能...