基于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居然还不会写学生管理系统,真的给我丢脸啊,教他又不肯学,还 ...
随机推荐
- Matlab状态模式
状态模式就是将状态的条件判断语句转化成其函数重写形式,利用了面向对象语言的多态性,本文根据https://blog.csdn.net/lm324114/article/details/78819602 ...
- Position定位相关知识了解
一.定位 position属性 1.默认定位: position:static; 元素框正常生成.块级元素生成一个矩形框,作为文档流的一部分,行内元素则会创建一个或多个行框,置于其 ...
- 使用 Create-React-App 开发 Chrome 扩展
整理 Kindle 标注.书签和笔记从未如此简单! Kindle 标注管理应用 Kindle Mate 只支持 Windows,不支持 Mac.标注只是解析我的剪贴文本文件,配合 FileReader ...
- Js 打印 div
var wind = window.open("", 'newwindow', 'height=700, width=1000, top=100, left=100, toolba ...
- Gitlab配置webhooks实现自动化部署
Gitlab 自动化部署 原理介绍 配置gitlab当push动作的时候,访问服务器上的一个链接比如www.shenke.group/hook.php hook.php里面写着一行代码,会让服务器gi ...
- 一行Python代码画心型
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/yanlaifan/article/deta ...
- PHP提示 Notice: Undefined variable
PHP提示Notice: Undefined variable,意思是:你的程序中有未定义的变量 为什么在其他地方好好的程序,换个环境报这个Notice,因为php.ini提醒级别设置的问题 场景复原 ...
- 【Spring Boot】Spring Boot之跨域解决方案
一.什么是跨域 跨域,指的是从一个域名去请求另外一个域名的资源.即跨域名请求!跨域时,浏览器不能执行其他域名网站的脚本,是由浏览器的同源策略造成的,是浏览器施加的安全限制. 跨域的严格一点来讲就是只要 ...
- Python的logging模块详解
Python的logging模块详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.日志级别 日志级别指的是产生的日志的事件的严重程度. 设置一个级别后,严重程度 ...
- MySQL/MariaDB数据库的各种日志管理
MySQL/MariaDB数据库的各种日志管理 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.事务日志 (transaction log) 1>.Innodb事务日志相 ...