基于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居然还不会写学生管理系统,真的给我丢脸啊,教他又不肯学,还 ...
随机推荐
- linq根据英文首字母姓名排序
names.Sort((a, b) => a.name.CompareTo(b.name));
- git安装用法和常用命令
目录 git 下载 安装 git 工作中常用命令 Git常用命令汇总以及其它相关操作 或者 常出错误: 常用修改: 题外话(相关概念) git 下载 安装 1.下载Git,官网地址:https://g ...
- 【转载】C#中List集合使用GetRange方法获取指定索引范围内的所有值
在C#的List集合中有时候需要获取指定索引位置范围的元素对象来组成一个新的List集合,此时就可使用到List集合的扩展方法GetRange方法,GetRange方法专门用于获取List集合指定范围 ...
- 学习笔记之大数据(Big Data)
300 秒带你吃透大数据! https://mp.weixin.qq.com/s/VWaqRig6_JBNYC1NX7NQ-Q 手把手教你入门Hadoop(附代码&资源) https://mp ...
- 设计的一些kubernetes面试题
公司现在上了一部分的业务至k8s,老实说,我心里很慌,在项目改造中,每天都会遇到很多问题,好友找我出一份k8s面试题,参考了网上的一些,再加上自己公司遇到的一些问题,整理如下: 参考链接:http:/ ...
- oracle之PLSQL导出-导入-表-存储过程等操作--亲测好用
1.背景 实际开发中考虑到安全,不会将生产库的数据和本地开发数据进行同步操作,而是采用导入导出sql语句的方式操作; 例如在开发环境写好的存储过程要更新到生产环境,那么就需要使用导出和导入功能. 2. ...
- Cannot assign to read only property 'exports' of object '#<Object>' ,文件名大小写问题!!!
有些坑不知道怎么就掉进去,可能一辈子都爬不起来!!! 一.错误描述 昨天还好好的,今天早上来从git获取了一下别人提交的代码就出错了!而提交代码的人 运行一点错误都没有!!! cya@KQ-101 M ...
- 手动实现KNN算法
手动实现KNN算法 计算距离 取k个邻近排序 距离(欧氏) 预习 import numpy as np # 数组运算是面向元素级别的 arr1 = np.array([1,2,3]) arr2 = n ...
- Linux-负载均衡HAproxy
负载均衡之HAProxy 现在常用的三大开源软件负载均衡器分别是Nginx.LVS.HAProxy.三大软件特点如下: LVS负载均衡的特点: ()抗负载能力强,抗负载能力强.性能高.能达到F5硬件的 ...
- ES6中Number中的扩展
1.Number.parseInt() , Number.parseFloat() 在ES6中将parseInt()和parseFloat()都移植到Number对象上去,方法的行为保持不变. // ...