概述
pymsql是Python中操作MySQL的模块,其使用方法和py2的MySQLdb几乎相同。
# coding=utf-8
import pymysql
conn=pymysql.connect(host='localhost',port=3306,user='root',passwd='123',db='s4') #连接mysql,必须存在数据库
cursor=conn.cursor(cursor=pymysql.cursors.DictCursor) #获取光标,括号里面的参数是使得到的数据以{'字段':数据}的形式输出,默认是以元组输出
r1=cursor.execute('create table test(id int primary key auto_increment,name varchar(20))')# execute即使执行mysql语句
r2=cursor.execute("insert into test values ('alex'),('bart'),('alvin'),('lily')")#返回值是添加的行数
print('r2=',r2)
ret=cursor.execute('select * from test')
f1=cursor.fetchone()#fetchone()取一条记录
print('f1=',f1)
f2=cursor.fetchmany(2)#fetchmany(number)取n条数据
print('f2=',f2)
cursor.scroll(-2,mode='relative')#移动光标位置,relative为相对原来位置
f3=cursor.fetchone()
print('f3=',f3)
cursor.scroll(1,mode='absolute')#移动光标,absolute为与开头的绝对位置
f4=cursor.fetchall()
print('f4=',f4)
conn.commit() #只有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 (25),
balance double
);
insert into account values (1,'alex',8000),(2,'ego',8000);
-- +----+------+---------+
-- | id | name | balance |
-- +----+------+---------+
-- | 1 | alex | 8000 |
-- | 2 | ego | 8000 |
-- +----+------+---------+
start transaction ;--开始事务
update account set balance=balance-5000 where name='alex';
select * from account;
-- +----+------+---------+ --此时数据并没有写入数据库,只是显示命令的结果,除非在操作下面写commit
-- | id | name | balance |
-- +----+------+---------+
-- | 1 | alex | 3000 |
-- | 2 | ego | 8000 |
-- +----+------+---------+
savepoint update1; --设置保留点
update account set balance=balance+5000 where name='ego';
select * from account;
-- +----+------+---------+ --一样数据没有写入数据库
-- | id | name | balance |
-- +----+------+---------+
-- | 1 | alex | 3000 |
-- | 2 | ego | 13000 |
-- +----+------+---------+
savepoint update2;
rollback to update1; --回滚至操作update1处,update1以上的操作任然存在,update1下的操作将全被取消
select * from account;
-- +----+------+---------+
-- | id | name | balance |
-- +----+------+---------+
-- | 1 | alex | 3000 |
-- | 2 | ego | 8000 |
-- +----+------+---------+
rollback ; --直接回滚,则会回滚自前面的commit处,如果没有commit就一直回滚至开头
-- +----+------+---------+
-- | id | name | balance |
-- +----+------+---------+
-- | 1 | alex | 8000 |
-- | 2 | ego | 8000 |
-- +----+------+---------+
commit ; --提交数据,此时数据才真正写入数据库
select * from account;
-- +----+------+---------+
-- | id | name | balance |
-- +----+------+---------+
-- | 1 | alex | 8000 |
-- | 2 | ego | 8000 |
-- +----+------+---------+
python的pymysql开启事务
# coding=utf-8
import pymysql
#添加数据
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='', db='yyy')
cursor = conn.cursor()
try:
insertSQL0="INSERT INTO ACCOUNT2 (name,balance) VALUES ('bart',1000)"
insertSQL1="UPDATE account2 set balance=balance-30 WHERE name='alex'"
insertSQL2="UPDATE account2 set balance=balance+30 WHERE name='ego'"
cursor = conn.cursor()
cursor.execute(insertSQL0)
conn.commit()
cursor.execute(insertSQL1)
raise Exception #模拟出现错误
cursor.execute(insertSQL2)
cursor.close()
conn.commit()
except Exception as e:
conn.rollback() #回滚
conn.commit()
cursor.close()
conn.close()
最后
以上就是虚幻天空为你收集整理的PyMysql以及事务的全部内容,希望文章能够帮你解决PyMysql以及事务所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复