MySQL事务--6个步骤

第一步:1、开始事务

复制代码
START TRANSACTION

第二步:取消手动提交事务

复制代码
set autocommit=0

第三步:插入sql语句

复制代码
sql语句

第四步:回滚,ROLLBACK

出错 回滚 ROLLBACK ( MYISAM不支持,引擎记得修改为InnoDB)

复制代码
ROLLBACK

第五步:提交

复制代码
COMMIT

第六步:恢复自动提交

复制代码
set autocommit=1

//事务
	// 1、开始事务
	//START TRANSACTION;

	sql = "START TRANSACTION";

	re = mysql_query(&mysql, sql.c_str());
	if (re != 0)
	{
		cout << "failed mysql_error" << mysql_error(&mysql) << endl;
	}


	//2、手动提交事务
	//set auocommit=0;
	sql = "set autocommit=0";

	re = mysql_query(&mysql, sql.c_str());
	if (re != 0)
	{
		cout << "failed mysql_error" << mysql_error(&mysql) << endl;
	}

	//3、sql语句
	//插入三条语句,回滚
	for (int i = 0;i < 3;i++)
	{
		sql = "insert into t_vedio (name) values ('test three!')";

		re = mysql_query(&mysql, sql.c_str());
		if (re != 0)
		{
			cout << "failed mysql_error" << mysql_error(&mysql) << endl;
		}
	}

	//4、出错回滚 ROLLBACK  MYISAM不支持

	sql = "ROLLBACK";
	re = mysql_query(&mysql, sql.c_str());
	if (re != 0)
	{
		cout << "failed mysql_error" << mysql_error(&mysql) << endl;
	}
	//count=0
	//插入三条语句,回滚
	for (int i = 0;i <100;i++)
	{
		sql = "insert into t_vedio (name) values ('test three!')";

		re = mysql_query(&mysql, sql.c_str());
		if (re != 0)
		{
			cout << "failed mysql_error" << mysql_error(&mysql) << endl;
		}
	}


	//5、COMMIT
	sql = "COMMIT";
	re = mysql_query(&mysql, sql.c_str());
	if (re != 0)
	{
		cout << "failed mysql_error" << mysql_error(&mysql) << endl;
	}


	//6、恢复自动提交
	//set auocommit=1;
	sql = "set autocommit=1";
	re = mysql_query(&mysql, sql.c_str());
	if (re != 0)
	{
		cout << "failed mysql_error" << mysql_error(&mysql) << endl;
	}
相关推荐
帝吃藕和15 分钟前
MySQL 知识点复习- 6. ORDER BY, GROUP BY
mysql
zzz大王43 分钟前
sql 五十题 26-30
数据库·sql
小张程序人生1 小时前
《系统掌握 ShardingSphere-JDBC:分库分表、读写分离、分布式事务一网打尽》
java·mysql
小尧嵌入式1 小时前
QT软件开发知识点流程及记事本开发
服务器·开发语言·数据库·c++·qt
子夜江寒1 小时前
SQL 从基础操作到高级查询
数据库·sql
Dxy12393102161 小时前
MySQL快速入门
数据库·mysql
NaiLuo_451 小时前
MySQL表的约束
数据库·sql·mysql
kkkkkkkkl241 小时前
彻底讲清 MySQL InnoDB 锁机制:从 Record 到 Next-Key 的全景理解
数据库·mysql
DBA小马哥1 小时前
Oracle迁移中查询优化器原理解析与实战优化策略
数据库·oracle
gugugu.1 小时前
Redis Hash类型深度解析:结构、原理与实战应用
数据库·redis·哈希算法