当前位置: 首页 > news >正文

Mysql基础 03 pymysql库、事务命令

文章目录

  • 一、pymysql库
  • 二、事务命令

一、pymysql库

python关于mysql的API用pymysql库实现。pymysql是Python中操作MySQL的模块。先安装该模块:conda install pymysql


import pymysql#添加数据
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='s3')cursor = conn.cursor()sql = "create table test (id int, name varchar(20))"
sql = "insert into test values(1,'A1'),(2,'A2')"ret = cursor.execute(sql)   # 执行Sql语句
print(ret)  # 返回影响的行数
import pymysql#添加数据
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='s3')cursor = conn.cursor()# sql = "insert into test values(1,'A1'),(2,'A2')"
sql = "update test set name='BB' where id =1"ret = cursor.execute(sql)   # 执行Sql语句conn.commit()   # 插入/更新数据后,需要提交,数据库才能保存该数据
cursor.close()  # 关闭游标
conn.close()    # 关闭数据库连接
import pymysql#查询数据
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='s3')cursor = conn.cursor()cursor.execute("select * from test")    # 需要先执行该语句,才能执行后续语句one = cursor.fetchone()     # 查询游标后一条记录
print(one)many = cursor.fetchmany(2)  # 查询游标后两条语句
print(many)all = cursor.fetchall()     # 查询游标后,所有语句
print(all)conn.commit()   # 插入/更新数据后,需要提交,数据库才能保存该数据
cursor.close()  # 关闭游标
conn.close()    # 关闭数据库连接
import pymysql#查询数据
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='s3')cursor = conn.cursor()cursor.execute("SELECT * FROM TEST")    # 需要先执行该语句,才能执行后续语句one=cursor.fetchone()
print(one)cursor.scroll(-1,mode='relative')  # 相对当前位置,向前移动1行
one=cursor.fetchone()
print(one)cursor.scroll(3,mode='absolute') # 绝对位置移动,移动到第3行结尾
one=cursor.fetchone()   # 显示第4行
print(one)conn.commit()   # 插入/更新数据后,需要提交,数据库才能保存该数据
cursor.close()  # 关闭游标
conn.close()    # 关闭数据库连接
import pymysql# 查询数据
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='s3')# 更改获取数据结果的数据类型,默认是元组,可以改为字典
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)cursor.execute("SELECT * FROM TEST")    # 需要先执行该语句,才能执行后续语句one=cursor.fetchone()
print(one)  # 显示字典形式conn.commit()   # 插入/更新数据后,需要提交,数据库才能保存该数据
cursor.close()  # 关闭游标
conn.close()    # 关闭数据库连接

二、事务命令

事务指逻辑上的一组操作,组成这组操作的各个单元,要么全部成功,要么全部不成功。

start transaction 开启事务。

Rollback 回滚事务,即撤销指定的sql语句(只能回退insert delete update语句),回滚到上一次commit的位置。

Commit 提交事务,提交未存储的事务。

savepoint 保留点 ,事务处理中设置的临时占位符 你可以对它发布回退(与整个事务回退不同)

create table account (id int primary key auto_increment,name varchar(8),balance int
);insert into account (name,balance) values("AA",8000),("BB",8000);select * from account;start transaction;update account set balance=balance-5000 where id=1;select * from account; -- 此时如果重开一个会话框,并不会更新数据Rollback;   -- 就算不新开会话框,如果执行事务回滚语句,也不会更新数据。
start transaction;update account set balance=balance-5000 where id=1;select * from account;update account set balance=balance+5000 where id=2;commit; -- 提交后,数据更新永久改变。

python中调用数据库启动事务的方式

import pymysqlconn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123456', db='s3')cursor = conn.cursor()try:insertSQL0 = "INSERT INTO ACCOUNT (name,balance) VALUES ('CC',4000)"insertSQL1="UPDATE account set balance=balance-30 WHERE id=1"insertSQL2="UPDATE account set balance=balance+30 WHERE id=2"cursor = conn.cursor()cursor.execute(insertSQL0)conn.commit()cursor.execute(insertSQL1)# raise Exceptioncursor.execute(insertSQL2)conn.commit()except Exception as e:conn.rollback()conn.commit()cursor.close()
conn.close()

savepoint示例:

start transaction;update account set balance=balance-5000 where id=1;savepoint update1; -- 保留点可以任意起名update account set balance=balance+5000 where id=2;savepoint update2;delete from account where name="DD";savepoint delete1;select * from account;
rollback to delete1;    -- 回滚到savepoint delete1;这句之上,即没有任何变化rollback to update2;    -- 取消删除语句,及以下的语句
select * from account;rollback to update1;    -- 取消第二条更新语句,及以下的语句
select * from account;

http://www.mrgr.cn/news/69431.html

相关文章:

  • 《Python编程实训快速上手》第四天--字符串操作
  • 【Java 多线程】:线程状态 线程操作 线程同步
  • Oracle OCP认证考试考点详解082系列17
  • 如何处理模型的过拟合和欠拟合问题
  • python可视化进阶
  • 科研绘图系列:R语言文章组合图形(barplot scatterplot)
  • Gen-RecSys——一个通过生成和大规模语言模型发展起来的推荐系统
  • 青藤深度参编的终端安全国家标准正式发布
  • 电商系统中,如何解决部分商品在短时间大量访问的单一热点问题?------Range范围分片
  • rce代码层面
  • Asyncio是Python库,它允许我们使用async/await语法编写并发代码。学习如何使用此库编写异步代码。
  • 探索10款音频剪辑软件,让你轻松编辑声音。
  • 链表类算法【leetcode】
  • 记录一次性能优化流程
  • Controlnet作者新作IC-light V2:基于FLUX训练,支持处理风格化图像,细节远高于SD1.5
  • 【1】虚拟机安装
  • AI 写作(五)核心技术之文本摘要:分类与应用(5/10)
  • 从0开始学习机器学习--Day20--优化算法的思路
  • Sequelize+Sqlite3使用示例
  • “2048”游戏网页版html+css+js