基于python的学生管理系统(含数据库版本)
这次支持连接到后台的数据库,直接和数据库进行交互,实现基本的增删查改
#!/usr/bin/python3
# coding=utf-8
"""
***********************help document****************************************
Usage:
this is a simple student grades system,now is simple
when start the programming,you can do following operations
enter 'a' is to insert data into database
enter 'b' is to display all data in database
enter 'c' is to query the specify info in database
enter 'h' is to see this help dacument
enter 'd' is to delete someone student's info
enter nothing is to do nothing,you can enter again
simple help document is: "a--insert/b--display/c--query/h--help/d--delete/''--default"
Example:
please enter the OPcode: a then [enter]
***************************************************************************** """ #引入pymysql这个库用来连接数据据
import pymysql #打印帮助文档
def help_document():
print(__doc__) #在已有的数据库上创建数据库的表
def create_database():
#在mysql中创建数据库student_db
db = pymysql.connect('localhost', 'root', 'root')
name = 'student_db'
cursor = db.cursor()
cursor.execute('drop database if exists ' + name)
cursor.execute('create database if not exists ' + name)
db.close()
#在数据库student_db中创建数据表student_table
db = pymysql.connect('localhost', 'root', 'root', 'student_db')
cursor = db.cursor()
name = 'student_table'
cursor.execute('drop table if exists ' + name)
sql = """create table student_table(
name varchar(30) primary key not null,
age varchar(10),
id varchar(20),
grade varchar(10))"""
cursor.execute(sql)
db.commit()
db.close() #数据库的插入
def insert_db(name,age,id,grade):
db = pymysql.connect('localhost', 'root', 'root','student_db')
cursor = db.cursor()
sql = "insert into student_table (name,age,id,grade) values ('%s','%s','%s','%s')" % \
(name,age,id,grade)
cursor.execute(sql)
db.commit()
db.close() #打印数据库中所有数据
def display_db():
db = pymysql.connect('localhost', 'root', 'root', 'student_db')
cursor = db.cursor()
sql = "select * from student_table"
try:
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
name = row[0]
age = row[1]
id = row[2]
grade = row[3]
print("name: '%s',age: '%s',id: '%s',grade: '%s'" % (name,age,id,grade))
print("that's all diaplayed!")
except:
print('nothing has been displayed...')
db.close() #数据库的查找
def query_db(name):
db = pymysql.connect('localhost', 'root', 'root', 'student_db')
cursor = db.cursor()
sql = "select * from student_table where name = '%s' " % name
try:
cursor.execute(sql)
result = cursor.fetchall()
for row in result:
name1 = row[0]
age1 = row[1]
id1 = row[2]
grade1 = row[3]
print("name: '%s',age: '%s',id: '%s',grade: '%s'" % \
(name1,age1,id1,grade1))
print('the query is over!')
except:
print('can not query data!')
db.close() #更新数据库
def update_db(name,age,id,grade):
db = pymysql.connect('localhost', 'root', 'root', 'student_db')
cursor = db.cursor()
sql = "update student_table set age = '%s',id = '%s',grade = '%s' where name = '%s'" % \
(age,id,grade,name)
try:
cursor.execute(sql)
db.commit()
print('updated successfully!')
except:
print('can not update data!')
db.close() #数据库的删除
def delete_db(name):
db = pymysql.connect('localhost', 'root', 'root', 'student_db')
cursor = db.cursor()
sql = "delete from student_table where name = '%s'" % name
try:
cursor.execute(sql)
db.commit()
print('delete the student info successfully!')
except:
print('delete failed...')
db.close() # 实现switch-case语句
class switch(object):
def __init__(self, value):
self.value = value
self.fall = False def __iter__(self):
"""Return the match method once, then stop"""
yield self.match
raise StopIteration def match(self, *args):
"""Indicate whether or not to enter a case suite"""
if self.fall or not args:
return True
elif self.value in args: # changed for v1.5, see below
self.fall = True
return True
else:
return False #建立一个学生类
class student:
#构造函数
def __init__(self, name, age, id, grade):
self.next = None
self.name = name
self.age = age
self.id = id
self.grade = grade
#每个学生可以输出自己的信息
def show(self):
print('name:', self.name, ' ', 'age:', self.age, ' ', 'id:', self.id, ' ', 'grade:', self.grade) #建立一个学生列表类
class stulist:
#构造函数
def __init__(self):
self.head = student('', 0, 0, 0)
#输出数据库中所有的数据
def display(self):
display_db()
#新增学生数据
def insert(self):
print('please enter:')
name = input('name:')
age = input('age:')
id = input('id:')
grade = input('grade:')
insert_db(name, age, id, grade) #查询学生数据
def query(self):
name = input('please enter the name you want to query:')
query_db(name) #删除学生数据
def delete(self):
name = input("please enter the student'name you want to delete:")
delete_db(name) #主函数,程序的入口
def main():
stulist1 = stulist()
user_input = input('please enter the OPcode:')
while user_input:
print("a--insert/b--display/c--query/h--help/d--delete/''--default")
for case in switch(user_input):
if case('a'): #按下'a'键
stulist1.insert()
user_input = input('please enter the OPcode:')
break
if case('b'): #按下'b'键
stulist1.display()
user_input = input('please enter the OPcode:')
break
if case('c'): #按下'c'键
stulist1.query()
user_input = input('please enter the OPcode:')
break
if case('d'): #按下'd'键
stulist1.delete()
user_input = input('please enter your OPcode:')
break
if case('h'): #按下'h'键
help_document()
user_input = input('please enter your OPcode:')
break
if case(): # default
print('please enter the OPcode...')
user_input = input('please enter the OPcode:')
break if __name__ == "__main__":
#第一次运行程序需要建立新的数据库,需要运行下面注释的一行代码,下次运行得将其注释掉
#create_database()
main()
结果效果图:
输入'h'查看帮助文档

输入'b'来查询数据库数据:

这个版本还是很粗糙,访问速度也很慢,如果以后还有时间的话,我会做一个界面出来,改进下和数据库的交互方式。
基于python的学生管理系统(含数据库版本)的更多相关文章
- 基于FORM组件学生管理系统【中间件】
目的:实现学生,老师,课程的增删改查 models.py from django.db import models # Create your models here. class UserInfo( ...
- C语言学生管理系统(原版本)(自编)
/*系统特色:(大牛勿笑) *颜色提示 *文字提示 *功能 */ #include <stdio.h> #include <stdlib.h> #include <mat ...
- 1、纯python编写学生信息管理系统
1.效果图 2.python code: class studentSys(object): ''' _init_(self) 被称为类的构造函数或初始化方法, self 代表类的实例,self 在定 ...
- 饮冰三年-人工智能-Python-26 Django 学生管理系统
背景:创建一个简单的学生管理系统,熟悉增删改查操作 一:创建一个Django项目(http://www.cnblogs.com/wupeiqi/articles/6216618.html) 1:创建实 ...
- 基于BootStrap,FortAweSome,Ajax的学生管理系统
一. 基于BootStrap,FortAweSome,Ajax的学生管理系统代码部分 1.students.html <1>html页面文件 <!DOCTYPE html> & ...
- Python连接SqlServer+GUI嵌入式——学生管理系统1.0
学生管理系统1.0 1.建学生数据库 2.数据库嵌入高级语言(Python) 3.界面设计 简化思路: 1.先通过SqlServer2012建立学生数据库,包括账号.密码,姓名.选课等信息 2.运用P ...
- 基于Python的SQL Server数据库对象同步轻量级实现
缘由 日常工作中经常遇到类似的问题:把某个服务器上的某些指定的表同步到另外一台服务器.类似需求用SSIS或者其他ETL工作很容易实现,比如用SSIS的话就可以,但会存在相当一部分反复的手工操作.建源的 ...
- 基于JSP的学生考勤管理系统(MySQL版)
介绍:基于JSP的学生考勤管理系统(MySQL版)1.包含源程序,数据库脚本.代码和数据库脚本都有详细注释.2.课题设计仅供参考学习使用,可以在此基础上进行扩展完善.开发环境:Eclipse ,MyS ...
- 如何用python做出老师看了都给满分的GUI学生管理系统毕设
序 言 哈喽大家好鸭!我是小熊猫 最近有什么有趣的事情发生吗?快来说给我听听( •̀ ω •́ )✧表弟大学快毕业了,学了一个学期Python居然还不会写学生管理系统,真的给我丢脸啊,教他又不肯学,还 ...
随机推荐
- ubuntu16.04 打开chrome弹出“Enter password to unlock your login keyring”解决方法
问题如图 输入开机密码发现验证失败. 解决 命令: find ~/ -name login.keyring 查找相关文件. 命令: sudo rm -rf /home/la/.local/share/ ...
- MVC中Model BLL层Model模型互转
MVC中Model BLL层Model模型互转 一. 模型通常可以做2种:充血模型和失血模型,一般做法是模型就是模型,不具备方法来操作,只具有属性,这种叫做失血模型(可能不准确):具备对模型一定的简单 ...
- Java 之 Maven 常用命令
一.Maven 常用命令 在 cmd 中可以通过一系列的 maven 命令对我们的 maven 工程进行编译.测试.运行.打包.安装和部署. 1.compile 命令 compile 是 maven ...
- 笔谈 cocoapods的安装与使用
因为要重构播放器库,所以就需要参考网上的开源项目,在播放器开源项目这块,kxmovie开源项目是值得参考的一个项目.在github下载下来后,运行该工程,发现其用到了cocoapods来管理第三方库, ...
- oracle 如何预估将要创建的索引的大小
一.1 oracle 如何预估将要创建的索引的大小 oracle 提供了2种可以预估将要创建的索引大小的办法: ① 利用包 Dbms_space.create_index_cost 直接得到 ② ...
- ABP问题集结
添加js跨域访问. 在Startup.cs文件中 public IServiceProvider ConfigureServices(IServiceCollection services)中添加 ...
- PDF 补丁丁 0.6.2.3572 测试版发布
近日有网友反馈 3571 测试版的补丁丁无法正常工作.请下载新发布的 3572 版本.
- 大数据之kafka-02.搞定kafka专业术语
02.搞定kafka专业术语 在kafka的世界中有很多概念和术语是需要我们提前理解并且熟练掌握的,下面来盘点一下. 之前我们提到过,kafka属于分布式的消息引擎系统,主要功能是提供一套完善的消息发 ...
- golang静态编译
golang 的编译(不涉及 cgo 编译的前提下)默认使用了静态编译,不依赖任何动态链接库. 这样可以任意部署到各种运行环境,不用担心依赖库的版本问题.只是体积大一点而已,存储时占用了一点磁盘,运行 ...
- Python练习题——用列表的方法输出杨辉三角
def main(): num = int(input('请输入行数: ')) yh = [[]] * num #创建num行空列表 for row in range(len(yh)): #遍历每一行 ...