python3数据库mysql增删改查
来源:互联网 发布:spark编程模型 编辑:程序博客网 时间:2024/06/11 15:51
#导入python和mysql的程序驱动import pymysql#import sys#reload(sys)#sys.setdefaultencoding( "utf-8" )#一、创建数据库对象connectionconn = pymysql.connect( host = 'localhost', user = 'root', passwd = '', charset='utf8')#二、创建数据库数据承载对象--获取游标对象cursorcursor = conn.cursor()#1.创建一个数据库--教务系统#cursor.execute('create database if not exists School ')#2.选择数据库conn.select_db('School')#3.删除数据库#cursor.execute('drop database if exists School')#4.创建数据库中的表cursor.execute('create table if not exists Student(Sno char(10)not null,Sname char(10),Ssex char(2),Sage int,Sdept varchar(20))')#5.删除表#cursor.execute('drop table if exists Student')#6.显示创建的数据库中的表cursor.execute('select* from Student')#得到一元祖 存放查询数据def display(): a=cursor.fetchone() print(a)#7.插入数据def insert(): cursor.execute('insert into Student(Sno,Sname,Ssex,Sage,Sdept)values("95001","李勇","男",20,"CS")') cursor.execute('insert into Student(Sno,Sname,Ssex,Sage,Sdept)values("95002","刘晨","女",19,"IS")') cursor.execute('insert into Student(Sno,Sname,Ssex,Sage,Sdept)values("95003","王敏","女",18,"MA")') cursor.execute('insert into Student(Sno,Sname,Ssex,Sage,Sdept)values("95004","张立","男",19,"IS")') cursor.execute('insert into Student(Sno,Sname,Ssex,Sage,Sdept)values("11111","王婵","男",19,"IS")')#提交到数据库 如果没有这一步就无法保存在数据库中#8.查询数据库所有字段def ALLdisplay(): count=cursor.execute('select* from Student') print('共有'+str(count)+'条记录') rows = cursor.fetchall() print('-----------------------------------') #遍历元祖输出数据 for row in rows: print(row[0],row[1],row[2],row[3],row[4])#五个属性#9.删除数据:def delete(): cursor.execute('delete from Student where Sno = "11111" ') conn.commit()#10.改数据:def redata(): cursor.execute('update Student set Ssex="女"where Sname="李勇"') conn.commit()#查询数据def find(): cursor.execute('select* from Student where Sno="95003"')find()redata()ALLdisplay()#三、关闭游标cursor.close#四、关闭对象conn.close
阅读全文
0 0
- python3操作mysql数据库增删改查
- python3数据库mysql增删改查
- MySql数据库增删改查
- python3增删改查
- Python3.6 连接mysql 数据库,增删改查,及多线程简单运用
- python3.6 使用 pymysql 连接 Mysql 数据库及 简单的增删改查操作
- python3.6 使用 pymysql 连接 Mysql 数据库及 简单的增删改查操作
- python3.6 使用 pymysql 连接 Mysql 数据库及 简单的增删改查操作
- mysql数据库的增删查改
- 对mysql数据库的增删改查
- mysql数据库增删改查示例
- MySql数据库增删改查示例
- Mysql数据库的增删改查
- mySql数据库操作 增删改查
- MySQL数据库的增删改查
- MySQL数据库的增删改查
- 数据库(增删改查、mysql建课程表)
- 快速教学mysql数据库,增删改查
- android studio 项目内部依赖 jar包生成
- 【2017.11.15】色彩的奥秘
- c语言:代码实现折半(二分)查找
- 面试基础知识
- myEclipse块操作
- python3数据库mysql增删改查
- Mybatis关联查询
- kNN python 註釋
- 面向对象的设计原则(一)
- 如何自己下载Java矩阵运算包-jama
- 创建嵌套类
- 关系运算和逻辑运算的应用
- 文章标题
- 软键盘弹出顶起布局的小技巧