创建数据库实例

import pymysql
db= pymysql.connect(host="localhost",user="root",password="1234",charset="utf8")
cursor=db.cursor()
cursor.execute("create database 数据库名 default charset=utf8;")
db= pymysql.connect(host="localhost",user="root",password="000000",charset="utf8",db="数据库名")
cursor=db.cursor()
cursor.execute('''
create table 表名(
SNO varchar(20),
SNAME char(5) ,
SAGE int(5),
SSEX char(5),
SACADEMY char(10),
SGRADE char(10),
SCLASS char(5),
primary key(SNO)
)default charset=utf8;
''')
cursor.execute('''
insert into 表名 values('2016081111','张三','20','男','软件工程学院','2016','3'),('2016061111','王杰','21','男','网络工程学院','2016','3'),
('2016071113','周顺','19','男','大气科学学院','2016','3'),('2017081180','李伟','20','男','软件工程学院','2017','2'),('2016081201','王丽','20','女','软件工程学院','2016','5');
db.commit()
cursor.execute('select * from 表名')
for i in cursor.fetchall():
print(i)
cursor.close()
db.close()

功能搭建

import pymysql
DB=None
class Student():
def select(self):
cursor=DB.cursor()
try:
SQL="select * from student"
cursor.execute(SQL)
for i in cursor.fetchall():
print(i)
except Exception as e:
print("报错",e)
finally:
cursor.close()
#查询
def Cha(self):
try:
cursor = DB.cursor()
a=input("请输入学号:")
cursor.execute("select * from student where SNO=%s"%a)
if cursor.fetchall():
SQL = "select * from student where SNO=%s"%a
cursor.execute(SQL)
for i in cursor.fetchall():
print(i)
else:
print("\n查询失败\n")
except Exception as e:
print("报错",e)
finally:
cursor.close()
#添加
def add(self):
cursor = DB.cursor()
a = input("输入添加的学号:")
b = input("输入添加的姓名:")
c = input("输入添加的年龄:")
d = input("输入添加的性别:")
e = input("输入添加的院系:")
f = input("输入添加的年级:")
g = input("输入添加的班级:")
try:
SQL = "insert into student values ('%s','%s','%s','%s','%s','%s','%s');" % (a, b, c, d, e, f, g)
cursor.execute(SQL)
DB.commit()
print("\n添加成功\n")
except Exception as e:
print("报错",e)
finally:
cursor.close()
#修改
def modify(self):
cursor = DB.cursor()
a = input("输入您要修改的学号:")
b = input("姓名修改为:")
c = input("年龄修改为:")
d = input("性别修改为:")
e = input("学院修改为:")
f = input("年级修改为:")
g = input("班级修改为:")
try:
SQL = "update student set SNAME='%s',SAGE='%s',SSEX='%s',SACADEMY='%s',SGRADE='%s',SCLASS='%s' where SNO='%s';" % (b,c,d,e,f,g,a)
cursor.execute(SQL)
DB.commit()
print("\n修改成功\n")
for i in cursor.fetchall():
print(i)
except Exception as e:
print("报错",e)
finally:
cursor.close()
#删除
def drop(self):
try:
cursor = DB.cursor()
a=input("输入您要删除的学生学号:")
cursor.execute("select * from student where SNO=%s"%a)
if cursor.fetchall():
SQL="delete from student where SNO=%s" % a
cursor.execute(SQL)
DB.commit()
print("\n删除成功\n")
for i in cursor.fetchall():
print(i)
else:
print("\n删除失败,可能没有该学号\n")
except Exception as e:
print("报错",e)
finally:
cursor.close() def main():
global DB
student=Student()
DB=pymysql.connect(host="localhost",user="root",password="1234",charset="utf8",db="zzz")
choose=input("请选择您的操作\n"
"1.查询所有学生的信息\n"
"2.按学号查询学生的信息\n"
"3.添加学生信息\n"
"4.修改学生信息\n"
"5.删除学生信息\n"
"6.退出\n")
while choose != "6":
if choose == "1":
student.select() elif choose == "2":
student.Cha() if choose == "3":
student.add() if choose == "4":
student.modify() if choose == "5":
student.drop() choose = input("请选择您的操作\n"
"1.查询所有学生的信息\n"
"2.按学号查询学生的信息\n"
"3.添加学生信息\n"
"4.修改学生信息\n"
"5.删除学生信息\n"
"6.退出\n")
else:
print("\n您已退出学生信息表\n") main()

最新文章

  1. Python操作Mysql之基本操作
  2. 【JAVA】 UIMnager
  3. YUM源
  4. 【原创】WinForm 模态对话框
  5. JS在IE和FireFox之间常用函数的区别小结
  6. ES6(ECMAScript 2015) 编码规范与详细注意要点
  7. cocos2d-x游戏开发之动画
  8. jQuery ajax的traditional参数的作用
  9. LayoutDemo
  10. html + CSS
  11. 移植MonkeyRunner的图片对比和获取子图功能的实现-Appium篇
  12. POJ 3307 Smart Sister
  13. JavaWeb一小时急速入门总结反思
  14. 我的java之路week2类的无参、带参方法
  15. Vue+abp微信扫码登录
  16. 解决Geoserver请求跨域的几种思路
  17. vins-mono中的imu参数设置
  18. PAT 乙级 1072 开学寄语(20 分)
  19. ubuntu开启root登陆
  20. 数组转换list集合问题

热门文章

  1. 如何从0开发一个Vue组件库并发布到npm
  2. MySQL-2-DQL
  3. go int64传到前端导致溢出问题排查
  4. rhel6下eth1恢复eth0
  5. python小题目练习(四)
  6. 说什么也要脱单——Python WEB开发:用Tornado框架制作简易【表白墙】网站
  7. java SWT中Label实时刷新当前时间
  8. NC23053 月月查华华的手机
  9. 019(The XOR Largest Pair)(字典树)
  10. java反序列化漏洞专项