吐槽一下
经过一段时间学习pymysql的经历,我深刻的体会到了pymysql的不靠谱之处;
就是在使用int型传参,我写的sql语句中格式化%d了之后,我在要传入的数据传递的每一步的去强制转换了,但是他还是会报错,说我的传入参数是string,他要int,我真笑了,最后把%d换成%s结果可以了,,,然后经过我查资料才了解到pymysql他这个库会去自动匹配类型,那也就是说在使用过程中可以对数据类型的关注可以降低一点,让库来给我解决数据类型不匹配的问题。
好了,吐槽就完了,然后就是现在的代码以及内容展示了
可视化展示
登录
在这里会去获取mysql中数据库db1的表user中的数据,然后会进行判断,成立下一步,不成立,会弹出弹窗,进行警告,成立就进入菜单
然后点击左上角的”点击进行操作吧“下面的按钮是假的哈哈哈哈哈哈哈哈哈。
编辑操作
这里删除之后需要关闭系统,可能是因为我这里有缓存的原因
查询操作
在这里去查询所有信息就会返回如图弹窗
按学号查询
按姓名查询
按性别查询
按年龄查询
按专业查询
别问我为什么不在这里去做个选择菜单,那就和性别一样了,懒得再进行加工了
按照班级查询
代码
main文件
from GUI import GUIif __name__ == '__main__':gui = GUI()gui.get_in()
GUI文件
import PySimpleGUI as sg
from LoginDatabase import LoginDatabase
from Editor import Editor
from Find import Findsg.theme('BlueMono')class GUI(object):def __init__(self):self.db_login_conn = LoginDatabase()self.db_login = self.db_login_conn.login_database_connect_gui()self.find = Find()self.editor = Editor()def get_in(self):layout = [[sg.Image(r'E:\Student_Manager_MySQL\th.png')],[sg.T('账号'), sg.InputText(default_text='1', key='first')],[sg.T('密码'), sg.InputText(default_text='1', key='second')],[sg.B('登录', key='login', button_color='blue'), sg.B('重置', key='rollback', button_color='blue')]]window = sg.Window('登录框', layout, text_justification='center')while True:event, values = window.read()if event is None:breakif event == 'login':sg.popup_auto_close('进入检测中,请等待')if values['first'] == self.db_login[0] and values['second'] == self.db_login[1]:sg.popup_auto_close("账号密码正确,请做好准备,即将进入系统——3 2 1 ")self.menu()else:sg.popup_auto_close('输入账号或密码错误,你到底是谁,再错我可要报警了奥')if event == 'rollback':window['first'].update('')window['second'].update('')window.close()def menu(self):menu_def = [['点击进行操作吧', ['添加学生', '删除学生', '修改学生', '查询学生']]]layout = [[sg.Image(r'E:\Student_Manager_MySQL\th.png')],[sg.T('您好,欢迎您使用本系统,请进行操作', size=(20, 5), font=('宋体', 15))],[sg.Menu(menu_def, key='manage_student', size=(20, 20), font=(15, 20))],[sg.B('点击进入系统', size=(10, 5), button_color='blue', key='点击进入系统'),sg.B('点击退出系统', size=(10, 5), button_color='red', key='点击退出系统')]]window = sg.Window('菜单', layout=layout)while True:event, values = window.read()if event in (None, '退出'):breakif event == '添加学生':sg.popup_auto_close('你就添加吧,等一会再告诉你怎么写')self.add_student_gui()if event == '查询学生':self.find_student_gui()if event == '修改学生':self.change_student_gui()if event == '删除学生':self.delete_student_gui()if event == '点击进入系统':sg.popup_auto_close('被骗了吧,哈哈哈哈哈', button_color='red')if event == '点击退出系统':sg.popup_auto_close('这是真的退出了')breakwindow.close()# 添加学生信息def add_student_gui(self):layout = [[sg.T("姓名"), sg.In(default_text='请不要设置过长', key='-name-')],[sg.T("性别"), sg.OptionMenu(values=['男', '女'], key='-gender-')],[sg.T("年龄"), sg.In(default_text='请输入整数(必须输入内容)', key='-age-')],[sg.T("专业"), sg.OptionMenu(values=['软件工程', '物联网工程', '大数据', '网络工程'], key='-major-')],[sg.T("班级"), sg.Combo(values=['1班', '2班'], key='-classroom-')],[sg.B(button_text="添加", key='-add-'), sg.B(button_text="退出", key='-exit-')]]window = sg.Window(title='添加学生', layout=layout)while True:event, values = window.read()if event is None:breakif event == '-exit-':breakif event == '-add-':# print(int(values['-age-']))# print(type(int(values['-age-'])))(self.editor.insert_student(values['-name-'], values['-gender-'],values['-classroom-'], int(values['-age-']), values['-major-']))sg.popup_auto_close('添加成功')breakwindow.close()# 查询学生信息def find_student_gui(self):layout = [[sg.B('按照学号查询', key='find_id')],[sg.B('按照姓名查询', key='find_name')],[sg.B('按照性别查询', key='find_gender')],[sg.B('按照年龄查询', key='find_age')],[sg.B('按照专业查询', key='find_major')],[sg.B('按照班级查询', key='find_classroom')],[sg.B('查询所有信息', key='find_all')]]window = sg.Window('查询学生', layout)while True:event, values = window.read()if event is None:breakif event == 'find_id':layout_id = [[sg.T('请输入要查询的学生学号id'), sg.In('', key='id')],[sg.B('确认', key='id_ok')]]window_id = sg.Window('查询学生id', layout_id)while True:event_id, values_id = window_id.read()if event_id is None:breakif event_id == 'id_ok':sg.popup(self.find.find_id(int(values_id['id'])))window_id.close()if event == 'find_name':layout_name = [[sg.T('请输入要查询的学生姓名name'), sg.In('', key='name')],[sg.B('确认', key='name_ok')]]window_name = sg.Window('查询学生姓名name', layout_name)while True:event_name, values_name = window_name.read()if event_name is None:breakif event_name == 'name_ok':print(values_name['name'])sg.popup(self.find.find_name(values_name['name']))window_name.close()if event == 'find_gender':layout_gender = [[sg.T('请输入要查询的学生性别gender'), sg.In('', key='gender')],[sg.B('确认', key='gender_ok')]]window_gender = sg.Window('查询学生性别gender', layout_gender)while True:event_gender, values_gender = window_gender.read()if event_gender is None:breakif event_gender == 'gender_ok':print(values_gender['gender'])sg.popup(self.find.find_gender(str(values_gender['gender'])))window_gender.close()if event == 'find_age':layout_age = [[sg.T('请输入要查询的学生年龄age'), sg.In('', key='age')],[sg.B('确认', key='age_ok')]]window_age = sg.Window('查询学生年龄age', layout_age)while True:event_age, values_age = window_age.read()if event_age is None:breakif event_age == 'age_ok':print(values_age['age'])sg.popup(self.find.find_age(int(values_age['age'])))window_age.close()if event == 'find_major':layout_major = [[sg.T('请输入要查询的学生专业major'), sg.In('', key='major')],[sg.B('确认', key='major_ok')]]window_major = sg.Window('查询学生专业major', layout_major)while True:event_major, values_major = window_major.read()if event_major is None:breakif event_major == 'major_ok':print(values_major['major'])sg.popup(self.find.find_major(str(values_major['major'])))window_major.close()if event == 'find_classroom':layout_classroom = [[sg.T('请输入要查询的学生班级classroom'), sg.In('', key='classroom')],[sg.B('确认', key='classroom_ok')]]window_classroom = sg.Window('查询学生班级classroom', layout_classroom)while True:event_classroom, values_classroom = window_classroom.read()if event_classroom is None:breakif event_classroom == 'classroom_ok':sg.popup(self.find.find_classroom(str(values_classroom['classroom'])))window_classroom.close()if event == 'find_all':result = self.find.get_all_students()sg.Popup(result)window.close()# 删除学生def delete_student_gui(self):layout = [[sg.T('请输入要删除的学生的学号'), sg.In('', key='id')],[sg.B('确认'), sg.B('取消')]]window = sg.Window('删除学生', layout)while True:event, values = window.read()if event is None:breakif event == '确认':self.editor.delete_student(int(values['id']))sg.popup_auto_close('删除成功')breakif event == '取消':breakwindow.close()# 修改学生信息def change_student_gui(self):layout = [[sg.T('请输入要修改的学生的学号'), sg.In(key='change_id')],[sg.B('确认'), sg.B('取消')]]window = sg.Window('修改学生', layout)while True:event, values = window.read()if event is None:breakif event == '确认':self.find.find_id(values['change_id'])name = self.find.find_id(int(values['change_id']))[0]gender = self.find.find_id(values['change_id'])[1]classroom = self.find.find_id(values['change_id'])[2]age = self.find.find_id(values['change_id'])[3]major = self.find.find_id(values['change_id'])[4]layout_change = [[sg.T('姓名'), sg.In(default_text=name, key='change_name')],[sg.T('性别'), sg.In(default_text=gender, key='change_gender')],[sg.T('班级'), sg.In(default_text=classroom, key='change_classroom')],[sg.T('年龄'), sg.In(default_text=age, key='change_age')],[sg.T('专业'), sg.In(default_text=major, key='change_major')],[sg.B('修改', key='change_ok')]]window_change = sg.Window(title='修改框', layout=layout_change)while True:event_change, values_change = window_change.read()if event_change is None:breakif event_change == 'change_ok':self.editor.update_student(student_id=values['change_id'], name=values_change['change_name'],gender=values_change['change_gender'],classroom=values_change['change_classroom'],age=values_change['change_age'], major=values_change['change_major'])sg.popup_auto_close('更新成功')breakwindow_change.close()if event == '取消':breakwindow.close()
DatabaseConnect文件
import pymysql
import jsonclass DatabaseConnect(object):def __init__(self):# 选择json文件进行存储必需数据with open(r'E:\Student_Manager_MySQL\database_login.json', 'r') as f:db_config = json.load(f)self.host = db_config['host']self.port = db_config['port']self.username = db_config['username']self.password = db_config['password']self.database = db_config['database']self.charset = db_config['charset']def connect_database(self):try:db = pymysql.connect(host=self.host,port=self.port,user=self.username,password=self.password,database=self.database,charset=self.charset)# print('数据库连接成功')# 返回dbreturn dbexcept pymysql.Error as e:print('数据库连接失败' + str(e))
LoginDatabase文件
from DatabaseConnect import DatabaseConnectclass LoginDatabase(object):def __init__(self):# 创建database_connect()类的对象self.db_connect = DatabaseConnect()# 给self.db赋值为dbself.db = self.db_connect.connect_database()def login_database_connect_gui(self):# 创建游标对象cur = self.db.cursor()# 写sql语句sqlquery = 'select * from user'# 执行SQL语句cur.execute(sqlquery)# 获取SQL语句的返回值results = list(cur.fetchall())# print(results)result = []for i in results:for j in i:result.append(j)# print(result)return result# print(results)
#
#
# db_connect = database_login()
# db_connect.login_database_connect_gui()
Editor文件
from LoginDatabase import LoginDatabaseclass Editor(LoginDatabase):def __init__(self):super().__init__()# 添加学生def insert_student(self, name, gender, classroom, age, major):# try:cur = self.db.cursor()values = (name, int(age), major, gender, classroom)sql = "INSERT INTO students (name, age, major, gender, classroom) VALUES (%s, %s, %s, %s, %s)"cur.execute(sql, values)self.db.commit()# xiube = '你回去检查一下你输入的年龄'# except Exception as e:# return xiube# 修改学生def update_student(self, student_id, name, age, classroom, gender, major):cur = self.db.cursor()sql = "UPDATE students SET name = %s, gender = %s, age = %s, major = %s, classroom = %s WHERE id = %s"values = (name, gender, age, major, classroom, student_id)cur.execute(sql, values)self.db.commit()# 删除学生def delete_student(self, student_id):cur = self.db.cursor()sql = "DELETE FROM students WHERE id = %s"cur.execute(sql, (student_id,))self.db.commit()
Find文件
from LoginDatabase import LoginDatabase# 查询信息
class Find(LoginDatabase):def __init__(self):super().__init__()# 访问所有学生def get_all_students(self):cur = self.db.cursor()cur.execute("SELECT * FROM students")results = list(cur.fetchall())print(results)return results# 访问iddef find_id(self, student_id):cur = self.db.cursor()sql = 'select name,gender,classroom,age,major from students where id=%s ' % student_idcur.execute(sql)list_1 = []result = list(cur.fetchall())for i in result:for j in i:list_1.append(j)return list_1# 访问姓名def find_name(self, name):cur = self.db.cursor()sql = 'select * from students where name like %s'cur.execute(sql, (f'%{name}%',))result = cur.fetchall()return result# 访问性别def find_gender(self, gender):cur = self.db.cursor()sql = 'select * from students where gender=%s'cur.execute(sql, gender)result = cur.fetchall()return result# 访问班级def find_classroom(self, classroom):cur = self.db.cursor()sql = 'select * from students where classroom like %s'cur.execute(sql, (f"%{classroom}%",))result = cur.fetchall()return result# 访问年龄def find_age(self, age):cur = self.db.cursor()sql = 'select * from students where age=%d' % agecur.execute(sql)result = cur.fetchall()return result# 访问专业def find_major(self, major):cur = self.db.cursor()sql = 'select * from students where major=%s'cur.execute(sql, major)result = cur.fetchall()return result
json文件
{"host": "localhost","port": 3306,"username": "root","password": "1234","database": "db1","charset": "utf8mb4"
}
# 如果要用json那就要把用户名username改成你的用户名,密码和数据库相应也要改成你的
关于可视化中图片
可视化界面中的图片是从网络找的,当然末尾也是找的;
结尾
好了,就到这里了,这一个版本的系统需求基本实现了,缺少一个用户类,然后细分管理员以及普通用户,但是系统功能都实现了,没关系,版本迭代是迟早的,如果有需要可以私信我要源码,如果之后版本迭代的系统我也写出来了,那当然可以给你了。
如有高见,请指教,如若有所收获,还是感激不尽。
灰太狼的大表哥-CSDN博客感谢灰太郎的大表哥给予我的一些关于pymysql的指导。