Python之创建数据库及功能示例样本


创建数据库实例
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之创建数据库及功能示例样本的更多相关文章
- Python MongoDB 创建数据库
章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...
- Python MySQL 创建数据库
章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...
- Python MongoDB 创建集合
章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...
- Python MySQL 创建表
章节 Python MySQL 入门 Python MySQL 创建数据库 Python MySQL 创建表 Python MySQL 插入表 Python MySQL Select Python M ...
- EntityFramework SQLiteCodeFirst 自动创建数据库 关闭级联删除
外键的级联删除: 如A表中有主键idA, B表中设置外键(ForeignKey)为A表中的主键idA, 当A表中的记录被删除时, B表中所有引用此条记录的记录(即所有外键为idA的记录)将自动被删除 ...
- Python操作Access数据库
我们在这篇文章中公分了五个步骤详细分析了Python操作Access数据库的相关方法,希望可以给又需要的朋友们带来一些帮助. AD: Python编 程语言的出现,带给开发人员非常大的好处.我们可以利 ...
- python操作Mysql数据库示例
python库:pymysql 安装:install pymysql.mysql数据库 一.连接数据库.创建speder库.查询版本. import pymysql ##链接数据库 db = pymy ...
- Python访问MySQL数据库并实现其增删改查功能
概述:对于访问MySQL数据库的操作,我想大家也都有一些了解.不过,因为最近在学习Python,以下就用Python来实现它.其中包括创建数据库和数据表.插入记录.删除记录.修改记录数据.查询数据.删 ...
- Python操作MySQL数据库完成简易的增删改查功能
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 目录 一丶项目介绍 二丶效果展示 三丶数据准备 四丶代码实现 五丶完整代码 一丶项目介绍 1.叙述 博主闲暇之余花了10个小时写的 ...
随机推荐
- Tensor的创建和维度的查看
常见的Tensor创建方法 1,基础Tensor函数:torch.Tensor(2,2)32位浮点型 2,指定类型: torch.DoubleTensor(2,2)64位浮点型 3,使用python的 ...
- MVVM,MVC,MVP的区别
MVC.MVP 和 MVVM 是三种常见的软件架构设计模式,主要通过分离关注点的方式来组织代码结构,优化开发效率. 在开发单页面应用时,往往一个路由页面对应了一个脚本文件,所有的页面逻辑都在一个脚本文 ...
- 封装环形加载进度条(Vue插件版和原生js版)
1.效果预览 2.用到的知识 主要利用SVG的stroke-dasharray和stroke-dashoffset这两个属性. 在看下面文章之前,你需要了解 <!DOCTYPE html> ...
- sap 调用Http 服务
REPORT ZMJ_GETAPI. DATA: LEN TYPE I, "发送报文长度 LEN_STRING TYPE STRING, URL TYPE STRING, "接口地 ...
- DNS原理&ssh
作用:实现域名的解析! www.baidu.com => 14.215.177.37 域名: www.baidu.com 实际域名为: www.baidu.com. 域名的解析,是反向的. 最后 ...
- Quick Pow: 如何快速求幂
今天讲个有趣的算法:如何快速求 \(n^m\),其中 n 和 m 都是整数. 为方便起见,此处假设 m >= 0,对于 m < 0 的情况,求出 \(n^{|m|}\) 后再取倒数即可. ...
- 不是吧?30秒 就能学会一个python小技巧?!
大家好鸭!我是小熊猫 很多学习Python的朋友在项目实战中会遇到不少功能实现上的问题,有些问题并不是很难的问题,或者已经有了很好的方法来解决.当然,孰能生巧,当我们代码熟练了,自然就能总结一些好用的 ...
- 018(Phone List)(字典树)
题目:http://ybt.ssoier.cn:8088/problem_show.php?pid=1471 题目思路: 这不就是一个超级明显的字典树嘛 字典树,又称单词查找树,Trie树,是一种树形 ...
- SSRS筛选器的IN运算(即包含于)用法
筛选器的IN运算,在Microsoft的官网上没像样儿的例子,不好设置,很容易错 Microsoft上的文档:https://docs.microsoft.com/zh-cn/sql/reportin ...
- mobaxterm会话同步
前言 之前用过MobaXterm,想不起来为啥不用了.后面主要还是用xshell,最近又在用WindTerm,WindTerm还不错,奈何有不少的Bug,所以又来研究一下MobaXterm 下午摸索了 ...