工具: Python 3.7
pycharm
mysql
本项目是对学生的基本增删改查(控制台版)
项目功能演示:https://download.csdn.net/download/zeal9s/10737062
项目的基本要求:https://blog.csdn.net/zeal9s/article/details/83274119
项目基本结构图:
dao包中的BaseDao类:
# -*- encoding:utf-8 -*-
"""
@作者:小思
@文件名:BaseDao.py
@时间:2018/10/19 8:51
@文档说明:连接数据库和关闭数据库的方法
"""
import pymysql
import com.zs.entity.Student as student
s = student.Student()
# 1.查询所有学生信息
def getAll():
connect = pymysql.connect(host="192.168.43.21", user="user", passwd="1234", db="zs")
cursor = connect.cursor()
cursor.execute("select * from Student")
data = cursor.fetchall()
connect.close()
return data
# 根据学生姓名查询学生信息
def getStudentBySname(s):
connect = pymysql.connect(host="192.168.43.21", user="user", passwd="1234", db="zs")
cursor = connect.cursor()
sql = "select * from Student where sname='%s'" % (s.getSname())
cursor.execute(sql)
data = cursor.fetchall()
connect.close()
return data
# 根据sid查询单个学生信息
def getStudentBySid(s):
connect = pymysql.connect(host="192.168.43.21", user="user", passwd="1234", db="zs")
cursor = connect.cursor()
sql = "select * from Student where sid=%d" % (s.getSid())
cursor.execute(sql)
data = cursor.fetchone()
connect.close()
return data
# 添加学生信息
def addStudent(s):
connect = pymysql.connect(host="192.168.43.21", user="user", passwd="1234", db="zs")
cursor = connect.cursor()
sql = "insert into Student values(null,'%s','%s','%d')" % (s.getSname(), s.getSsex(), s.getSage())
cursor.execute(sql)
connect.commit()
connect.close()
# 删除学生信息
def delStudent(s):
connect = pymysql.connect(host="192.168.43.21", user="user", passwd="1234", db="zs")
cursor = connect.cursor()
sql = "delete from Student where sid=%d" % (s.getSid())
cursor.execute(sql)
connect.commit()
connect.close()
# 修改学生信息
def updStudent(s):
connect = pymysql.connect(host="192.168.43.21", user="user", passwd="1234", db=