python 学生信息管理系统
python与数据库的例子
初始化数据库
链接数据库创建库和表并插入数据
init.py
import pymysql
sql_base='create database school;'
sql_table='''create table student(sno varchar(20) primary key,
sname char(10),
sage int(10),
sex char(4),
sacademy char(10),
sgrade char(9),
sclass char(10))default charset=utf8;'''
DB=pymysql.connect(host='localhost',passwd='1234',charset='utf8',user='root')
cursor=DB.cursor()
cursor.execute(sql_base)
cursor.execute('use school')
cursor.execute(sql_table)
sql_insert='''insert into student values('2016081111','张三',20,'男','软件工程学院','2016',3),
('2016061111','王杰',21,'男','网络工程学院','2016',3),
('2016071113','周顺',19,'男','大气科学学院','2016',3),
('2017081180','李伟',20,'男','软件工程学院','2017',2),
('2016081201','王丽',20,'女','软件工程学院','2016',5);'''''
cursor.execute(sql_insert)
DB.commit()
实现各种功能
func.py
DB=None
import pymysql
class Method():
def __init__(self):
self.qurey_sql='select * from student'
def qurey_all(self):
cursor=DB.cursor()
print('学号 \t 姓名 年龄 性别\t学院\t 学年 班级\t')
cursor.execute(self.qurey_sql)
info=cursor.fetchall()
for i in info:
if i != info[0]:
print()
for j in i:
print(j,end=' ')
cursor.close()
def qurey_sno(self):
try:
cursor=DB.cursor()
sno = input('请输入学号:')
print('学号 \t 姓名 年龄 性别\t学院\t 学年 班级\t')
sno_sql = 'select * from student where sno=%s'%(sno)
cursor.execute(sno_sql)
a=cursor.fetchall()[0]
for i in a:
print(i,end=' ')
except Exception as e:
print('有错误',e)
finally:
cursor.close()
def qurey_sname(self):
try:
cursor=DB.cursor()
sname = input('请输入姓名:')
print('学号 \t 姓名 年龄 性别\t学院\t 学年 班级\t')
sno_sql = 'select * from student where sname="%s"'%(sname)
cursor.execute(sno_sql)
for i in cursor.fetchall()[0]:
print(i ,end=' ')
except Exception as e:
print('有错误',e)
finally:
cursor.close()
def qurey_academy(self):
try:
cursor=DB.cursor()
academy = input('请输入学院:')
print('学号 \t 姓名 年龄 性别\t学院\t 学年 班级\t')
academy_sql = 'select * from student where sacademy="%s"' % (academy)
cursor.execute(academy_sql)
x=cursor.fetchall()
for i in x:
if i != x[0]:
print()
for j in i:
print(j,end=' ')
except Exception as e:
print('有错误',e)
finally:
cursor.close()
def add_info(self):
try:
cursor=DB.cursor()
info=list(input('请输入添加的信息 学号 姓名 年龄 性别 学院 年级 班级:\n(每项空格隔开)').split(' '))
sql_insert='''insert into student values('%s','%s',%d,'%s','%s','%s','%s')'''\
%(info[0],info[1],int(info[2]),info[3],info[4],info[5],info[6])
cursor.execute(sql_insert)
DB.commit()
except Exception as e:
print('有错误',e)
finally:
cursor.close()
def modify_info(self):
try:
cursor=DB.cursor()
sname=input('请输入修改学生的名字:')
sno_sql = 'select * from student where sname="%s"' % (sname)
cursor.execute(sno_sql)
print('学号 \t 姓名 年龄 性别\t学院\t 学年 班级\t')
for i in cursor.fetchall()[0]:
print(i, end=' ')
ch=input('\n请输入要修改的字段 :')
if ch == '学号':
ch = 'sno'
if ch == '姓名':
ch = 'sname'
if ch == '年龄':
ch = 'sage'
if ch == '性别':
ch = 'sex'
if ch == '学院':
ch = 'sacademy'
if ch == '学年':
ch = 'sgrade'
if ch == '班级':
ch = 'sclass'
context=input('请输入要修改的信息')
up_sql='update student set %s="%s" where sname="%s";'%(ch,context,sname)
cursor.execute(up_sql)
DB.commit()
if ch == 'sname':
sno_sql = 'select * from student where sname="%s"' % (context)
cursor.execute(sno_sql)
print('学号 \t 姓名 年龄 性别\t学院\t 学年 班级\t')
for i in cursor.fetchall()[0]:
print(i,end=' ')
else:
cursor.execute(sno_sql)
print('学号 \t 姓名 年龄 性别\t学院\t 学年 班级\t')
for i in cursor.fetchall()[0]:
print(i, end=' ')
except Exception as e:
print('错误',e)
finally:
cursor.close()
def delete(self):
try:
cursor=DB.cursor()
cursor.execute(self.qurey_sql)
for i in cursor.fetchall():
print(i)
del1=input('请输入要删除的学生姓名')
del_sql='delete from student where sname="%s"'%del1
#print(del_sql)
cursor.execute(del_sql)
DB.commit()
except Exception as e:
print('出错',e)
finally:
cursor.close()
def main():
global DB
DB=pymysql.connect(host='localhost',user='root',password='1234',charset='utf8',database='school')
method=Method()
print('请选择您的操作:\n1.查询所有学生的信息\t2.按学号查询学生的信息\t3.按姓名查询学生的信息\n4.按学院查询学生的信息\t5.添加学生信息'
'\t6.修改学生信息\n7.删除学生信息\t8.退出',end='')
while True:
c=input('\n请输入功能: (s=功能选项)')
if c == '1':
method.qurey_all()
if c == '2':
method.qurey_sno()
if c == '3':
method.qurey_sname()
if c == '4':
method.qurey_academy()
if c == '5':
method.add_info()
if c == '6':
method.modify_info()
if c == '7':
method.delete()
if c == '8':
DB.close()
print('感谢使用')
break
if c == 's':
print('请选择您的操作:\n1.查询所有学生的信息\t2.按学号查询学生的信息\t3.按姓名查询学生的信息\n'
'4.按学院查询学生的信息\t5.添加学生信息'
'\t6.修改学生信息\n7.删除学生信息\t8.退出', end='')
main()
python 学生信息管理系统的更多相关文章
- Python学生信息管理系统的开发
# 第一题:设计一个全局变量,来保存很多个学生信息:学生(学号, 姓名,年龄):思考要用怎样的结构来保存:# 第二题:在第一题基础上,完成:让用户输入一个新的学生信息(学号,姓名,年龄):你将其保存在 ...
- Python基础案例练习:制作学生信息管理系统
一.前言 学生信息管理系统,相信大家或多或少都有做过 最近看很多学生作业都是制作一个学生信息管理系统 于是,今天带大家做一个简单的学生信息管理系统 二.开发环境: 我用到的开发环境 Python 3. ...
- python 04 学生信息管理系统
今天任务不多,做了学生信息管理系统1.0,使用字典存储学生个体信息,列表存储学生字典.注意dict定义要在循环体内,若定义成全局变量或循环体外,则旧数据会被新数据覆盖.dict属于可变类型数据,内容改 ...
- 【python免费代码】设计一个简单的学生信息管理系统
文章目录 前言 一.理解 二.部分截图展示 三.代码 四.总结 前言 设计一个简单的学生信息管理系统,实现以下功能(bug) : 录入学生信息,信息以文件方式存储 以学生学号或者学生姓名为条件查询该学 ...
- python3 简陋的学生信息管理系统
# 编写一个“学生信息管理系统”# 输入序号:1. 输入学生信息,学生信息包括:id,name,age,gender(用什么数据类型保存?)# 2. 查询:输入学生姓名和id,显示学生个人信息# 3. ...
- 基于数组或链表的学生信息管理系统(小学期C语言程序实训)
1.基于数组的学生信息管理系统 实验内容: 编写并调试程序,实现学校各专业班级学生信息的管理.定义学生信息的结构体类型,包括:学号.姓名.专业.班级.3门成绩. 实验要求: (1) main函数:以菜 ...
- 基于数据库MySQL的简易学生信息管理系统
通过这几天学习Mysql数据库,对其也有了基本的了解,为了加深印象,于是就写了一个最简易的学生信息管理系统. 一:基本要求 1.通过已知用户名和密码进行登录: 2.可以显示菜单: 3.可以随时插入学生 ...
- C++ 简单的学生信息管理系统
// // main.cpp // 2013-7-17作业1 // // Created by 丁小未 on 13-7-17. // Copyright (c) 2013年 dingxiaowei. ...
- 学生信息管理系统v1.0
昨天一个教师朋友找到我,告诉我现在学期末他工作比较忙.需要统计处理很多学生信息,想让我帮他做一个管理系统.实现的功能就是把WPS表格转化成Word文档,将每一个学生的信息都能够分开,并且要根据名字找到 ...
随机推荐
- while,do...while及for三种循环结构
循环结构 while循环 while (布尔表达式) { //循环内容 } 只要布尔表达式为true循环就会一直执行 我们大多数情况会让循环停止下来,需要一个让表达式失效的方式来停止循环 while循 ...
- vite的项目,使用 rollup 打包的方法
官网资料 构建生产版本--库模式 https://cn.vitejs.dev/guide/build.html#library-mode 详细设置 https://cn.vitejs.dev/conf ...
- Redis概述以及Linux安装
Redis 概述 Redis是什么 Redis,Remote Dictionary Server,远程字典服务.是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.key-v ...
- P7045 「MCOI-03」金牌
考虑维护一个队列. 先插入\(a_1 = 0\) 依次往后考虑,如果和队列里相斥,则我们把队列一个和他捆绑起来. 如果队列空,则加入该颜色. 最后考虑往队列里插入改颜色. 总共为\(2 * (n - ...
- AT4168 [ARC100C] Or Plus Max
从\(whk\)回来了. 考虑我们需要维护一个子集的信息. 对于二进制的子集信息维护有一个很经典的操作: 高维前缀和. AT4168 [ARC100C] Or Plus Max // Problem: ...
- A Child's History of England.46
As, one hundred years before, the servile [卑躬屈膝的~serve] followers of the Court had abandoned the Con ...
- C/C++ Qt 数据库与SqlTableModel组件应用
SqlTableModel 组件可以将数据库中的特定字段动态显示在TableView表格组件中,通常设置QSqlTableModel类的变量作为数据模型后就可以显示数据表内容,界面组件中则通过QDat ...
- android获取路径目录方法
Environment常用方法: getExternalStrongeDirectory() 返回File,获取外部存储目录即SDCard getDownloadCacheDirectory() 返回 ...
- Linux基础命令---mget获取ftp文件
mget 使用lftp登录mftp服务器之后,可以使用mget指令从服务器获取文件.mget指令可以使用通配符,而get指令则不可以. 1.语法 mget [-E] [-a] [- ...
- alert之后才执行
如果在正常情况下,代码要在alert之后才执行,解决办法:将要执行的代码用setTimeout延迟执行即可(原因:页面未加载完毕)