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 学生信息管理系统的更多相关文章

  1. Python学生信息管理系统的开发

    # 第一题:设计一个全局变量,来保存很多个学生信息:学生(学号, 姓名,年龄):思考要用怎样的结构来保存:# 第二题:在第一题基础上,完成:让用户输入一个新的学生信息(学号,姓名,年龄):你将其保存在 ...

  2. Python基础案例练习:制作学生信息管理系统

    一.前言 学生信息管理系统,相信大家或多或少都有做过 最近看很多学生作业都是制作一个学生信息管理系统 于是,今天带大家做一个简单的学生信息管理系统 二.开发环境: 我用到的开发环境 Python 3. ...

  3. python 04 学生信息管理系统

    今天任务不多,做了学生信息管理系统1.0,使用字典存储学生个体信息,列表存储学生字典.注意dict定义要在循环体内,若定义成全局变量或循环体外,则旧数据会被新数据覆盖.dict属于可变类型数据,内容改 ...

  4. 【python免费代码】设计一个简单的学生信息管理系统

    文章目录 前言 一.理解 二.部分截图展示 三.代码 四.总结 前言 设计一个简单的学生信息管理系统,实现以下功能(bug) : 录入学生信息,信息以文件方式存储 以学生学号或者学生姓名为条件查询该学 ...

  5. python3 简陋的学生信息管理系统

    # 编写一个“学生信息管理系统”# 输入序号:1. 输入学生信息,学生信息包括:id,name,age,gender(用什么数据类型保存?)# 2. 查询:输入学生姓名和id,显示学生个人信息# 3. ...

  6. 基于数组或链表的学生信息管理系统(小学期C语言程序实训)

    1.基于数组的学生信息管理系统 实验内容: 编写并调试程序,实现学校各专业班级学生信息的管理.定义学生信息的结构体类型,包括:学号.姓名.专业.班级.3门成绩. 实验要求: (1) main函数:以菜 ...

  7. 基于数据库MySQL的简易学生信息管理系统

    通过这几天学习Mysql数据库,对其也有了基本的了解,为了加深印象,于是就写了一个最简易的学生信息管理系统. 一:基本要求 1.通过已知用户名和密码进行登录: 2.可以显示菜单: 3.可以随时插入学生 ...

  8. C++ 简单的学生信息管理系统

    // // main.cpp // 2013-7-17作业1 // // Created by 丁小未 on 13-7-17. // Copyright (c) 2013年 dingxiaowei. ...

  9. 学生信息管理系统v1.0

    昨天一个教师朋友找到我,告诉我现在学期末他工作比较忙.需要统计处理很多学生信息,想让我帮他做一个管理系统.实现的功能就是把WPS表格转化成Word文档,将每一个学生的信息都能够分开,并且要根据名字找到 ...

随机推荐

  1. [GZOI2017]配对统计

    发现我们可以在\(O(n)\)里很多处理出至多\(2n\)对好对. 然后转化成二维偏序. 然后想怎么做怎么做:排序+BIT,莫队都行.

  2. C++ and OO Num. Comp. Sci. Eng. - Part 3.

    2. Expressions and Statements 声明是将一个种类型的变量引入程序的语句. 作用域 作用域又一对花括号限定,在所有花括号之外的为全局作用域. 在作用域内声明的变量为局部变量. ...

  3. Perl 语言入门1-5

    第一章 简介 perl -v 文字处理,编写小型CGI脚本(Web服务器调用程序)的最佳语言 CPAN: Perl综合典藏网 shebang: #! /usr/bin/perl 或#! /usr/lo ...

  4. GWAS初探

    原理 GWAS 的主要方法学依据是归纳法中的共变法,是探究复杂因果关系最主要的科学思维和方法.所谓共变法,是通过考察被研究现象发生变化的若干场合中,确定是否只有一个情况发生相应变化,如果是,那么这个发 ...

  5. Metabolomics Workfolw

    推荐R语言界的国内大佬于淼写的代谢组学workflow,包含了大部分代谢组学(以及暴露组)的数据分析方法. Meta-Workflow 主要内容包括: Sample collection Pretre ...

  6. web性能测试工具——http_load

    http_load是一款基于Linux平台的web服务器性能测试工具,用于测试web服务器的吞吐量与负载,web页面的性能. http_load是基于linux.unix平台的一种性能测工具 它以并行 ...

  7. zabbix监控php状态

    环境介绍: php /usr/loca/php nignx /usr/loca/nginx  配置文件都是放在extra中 修改php-fpm的配置文件启动状态页面 pm.status_path = ...

  8. 搭建简单的SpringCloud项目三:问题及解决

    GitHub:https://github.com/ownzyuan/test-cloud 前篇:搭建简单的SpringCloud项目一:注册中心和公共层 搭建简单的SpringCloud项目二:服务 ...

  9. 全网最详细的ReentrantReadWriteLock源码剖析(万字长文)

    碎碎念) 花了两天时间,终于把ReentrantReadWriteLock(读写锁)解析做完了.之前钻研过AQS(AbstractQueuedSynchronizer)的源码,弄懂读写锁也没有想象中那 ...

  10. Mybatis相关知识点(一)

    MyBatis入门 (一)介绍 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code, ...