创建数据库实例

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()

Python之创建数据库及功能示例样本的更多相关文章

  1. Python MongoDB 创建数据库

    章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...

  2. Python MySQL 创建数据库

    章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...

  3. Python MongoDB 创建集合

    章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...

  4. Python MySQL 创建表

    章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...

  5. EntityFramework SQLiteCodeFirst 自动创建数据库 关闭级联删除

    外键的级联删除: 如A表中有主键idA, B表中设置外键(ForeignKey)为A表中的主键idA, 当A表中的记录被删除时, B表中所有引用此条记录的记录(即所有外键为idA的记录)将自动被删除 ...

  6. Python操作Access数据库

    我们在这篇文章中公分了五个步骤详细分析了Python操作Access数据库的相关方法,希望可以给又需要的朋友们带来一些帮助. AD: Python编 程语言的出现,带给开发人员非常大的好处.我们可以利 ...

  7. python操作Mysql数据库示例

    python库:pymysql 安装:install pymysql.mysql数据库 一.连接数据库.创建speder库.查询版本. import pymysql ##链接数据库 db = pymy ...

  8. Python访问MySQL数据库并实现其增删改查功能

    概述:对于访问MySQL数据库的操作,我想大家也都有一些了解.不过,因为最近在学习Python,以下就用Python来实现它.其中包括创建数据库和数据表.插入记录.删除记录.修改记录数据.查询数据.删 ...

  9. Python操作MySQL数据库完成简易的增删改查功能

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 目录 一丶项目介绍 二丶效果展示 三丶数据准备 四丶代码实现 五丶完整代码 一丶项目介绍 1.叙述 博主闲暇之余花了10个小时写的 ...

随机推荐

  1. Spring框架系列(2) - Spring简单例子引入Spring要点

    上文中我们简单介绍了Spring和Spring Framework的组件,那么这些Spring Framework组件是如何配合工作的呢?本文主要承接上文,向你展示Spring Framework组件 ...

  2. 编程技巧│提高 Javascript 代码效率的技巧

    目录 一.变量声明 二.三元运算符 三.解构赋值 四.解构交换 五.箭头函数 六.字符串模版 七.多值匹配 八.ES6对象简写 九.字符串转数字 十.次方相乘 十一.数组合并 十二.查找数组最大值最小 ...

  3. Java:如何打印整个字符串数组?

    例: public static void main(String[] args) { String prodName = "雇员姓名,雇员唯一号"; String[] prodN ...

  4. MYSQL索引的建立、删除以及简单使用

    一.前期数据准备 1.建表 CREATE TABLE `user` ( `uid` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(50) DEFAUL ...

  5. 【RocketMQ】消息的刷盘机制

    刷盘策略 CommitLog的asyncPutMessage方法中可以看到在写入消息之后,调用了submitFlushRequest方法执行刷盘策略: public class CommitLog { ...

  6. Linux查看内网服务器的出口IP

    查看内网服务器的出口IPcurl ifconfig.me [root@vpnserver ~]# curl ifconfig.me111.10.100.100 [root@vpnserver ~]#

  7. Graph-Based Social Relation Reasoning

    title: Graph-Based Social Relation Reasoning, 2020 task: we propose a simpler, faster, and more accu ...

  8. 【每天学一点-02】创建Node.js的第一个应用

    1.引入require模块,使用createServer()创建服务器 [server.js]文件 var http = require('http'); http.createServer(func ...

  9. 小试牛刀:Go 反射帮我把 Excel 转成 Struct

    背景 起因于最近的一项工作:我们会定义一些关键指标来衡量当前系统的健康状态,然后配置对应的报警规则来进行监控报警.但是当前的报警规则会产生大量的误报,需要进行优化.我所负责的是将一些和用户行为指标相关 ...

  10. 从零开始Blazor Server(9)--修改Layout

    目前我们的MainLayout还是默认的,这里我们需要修改为BootstrapBlazor的Layout,并且处理一下菜单. 修改MainLayout BootstrapBlazor已经自带了一个La ...