这次支持连接到后台的数据库,直接和数据库进行交互,实现基本的增删查改

 #!/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的学生管理系统(含数据库版本)的更多相关文章

  1. 基于FORM组件学生管理系统【中间件】

    目的:实现学生,老师,课程的增删改查 models.py from django.db import models # Create your models here. class UserInfo( ...

  2. C语言学生管理系统(原版本)(自编)

    /*系统特色:(大牛勿笑) *颜色提示 *文字提示 *功能 */ #include <stdio.h> #include <stdlib.h> #include <mat ...

  3. 1、纯python编写学生信息管理系统

    1.效果图 2.python code: class studentSys(object): ''' _init_(self) 被称为类的构造函数或初始化方法, self 代表类的实例,self 在定 ...

  4. 饮冰三年-人工智能-Python-26 Django 学生管理系统

    背景:创建一个简单的学生管理系统,熟悉增删改查操作 一:创建一个Django项目(http://www.cnblogs.com/wupeiqi/articles/6216618.html) 1:创建实 ...

  5. 基于BootStrap,FortAweSome,Ajax的学生管理系统

    一. 基于BootStrap,FortAweSome,Ajax的学生管理系统代码部分 1.students.html <1>html页面文件 <!DOCTYPE html> & ...

  6. Python连接SqlServer+GUI嵌入式——学生管理系统1.0

    学生管理系统1.0 1.建学生数据库 2.数据库嵌入高级语言(Python) 3.界面设计 简化思路: 1.先通过SqlServer2012建立学生数据库,包括账号.密码,姓名.选课等信息 2.运用P ...

  7. 基于Python的SQL Server数据库对象同步轻量级实现

    缘由 日常工作中经常遇到类似的问题:把某个服务器上的某些指定的表同步到另外一台服务器.类似需求用SSIS或者其他ETL工作很容易实现,比如用SSIS的话就可以,但会存在相当一部分反复的手工操作.建源的 ...

  8. 基于JSP的学生考勤管理系统(MySQL版)

    介绍:基于JSP的学生考勤管理系统(MySQL版)1.包含源程序,数据库脚本.代码和数据库脚本都有详细注释.2.课题设计仅供参考学习使用,可以在此基础上进行扩展完善.开发环境:Eclipse ,MyS ...

  9. 如何用python做出老师看了都给满分的GUI学生管理系统毕设

    序 言 哈喽大家好鸭!我是小熊猫 最近有什么有趣的事情发生吗?快来说给我听听( •̀ ω •́ )✧表弟大学快毕业了,学了一个学期Python居然还不会写学生管理系统,真的给我丢脸啊,教他又不肯学,还 ...

随机推荐

  1. C#泛型集合之——列表

    列表基础 1.列表概述:列表与哈希集合不同之处在于,它的元素可以重复.(更接近逻辑上的数组,而哈希集合更接近于数学上的集合) 2.创建及初始化: (1)List<类型> 列表名 =new ...

  2. Windows双系统

    基础概念 基础概念 Legacy:传统BIOS传输模式启动顺序:开机→BIOS初始化→BIOS自检→引导操作系统→进入系统.传统硬盘引导记录为MBR格式,MBR无法支持超过2T的硬盘.但拥有最好的兼容 ...

  3. css 和常用快捷键

    一.css概述: 1.规则:CSS 规则由选择器,以及一条或多条声明两个部分构成. 2.选择器:选择器通常是您需要改变样式的 HTML 元素. 3.声明:声明是您要设置的样式(每条声明由一个属性和一个 ...

  4. unity shader入门(三)逐像素光照,Blinn-Phong模型

    与上篇逐顶点光照很像,只是改为在片元着色器中计算光照,下为逐像素光照shader Shader "study/Chapter6/PixelShader"{ Properties{ ...

  5. Centos7安装DockerCE

    1. 说明 以下使用的系统为centos7,64位,镜像为CentOS-7-x86_64-Minimal-1804,所有操作以root用户操作 2. 安装Docker官方源 2.1 安装yum工具集 ...

  6. C++类中创建线程

    ​ 经常会遇到需要在类中创建线程,可以使用静态成员函数,并且将类实例的指针传入线程函数的方式来实现. 实现代码代码如下: /* 类头文件 CTestThread.h */ #include<io ...

  7. 【转】移植vsftpd到arm linux

    vsftpd即very secure FTP daemon(非常安全的FTP进程),是一个基于GPL发布的类UNIX类操作系统上运行的服务器的名字(是一种守护进程),可以运行在诸如Linux.BSD. ...

  8. 【HCIA Gauss】学习汇总-数据库管理(SQL语法 库表 索引操作)-5

    # 简单查询select * from table_reference # 创建表 create table TB(staff_id int primary key , course_name cha ...

  9. kubernetes 清理孤儿POD--转发

    孤儿pod的产生 节点OOM以后或者节点异常崩溃的情况下,pod未能被正常的清理而导致的孤儿进程. 提示如下 Orphaned pod found - but volume paths are sti ...

  10. ztree根据关键字模糊搜索

    html页面需要引入以下资源 <!-- jquery包,ztree依赖jquery --> <script type="text/javascript" src= ...