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;
	}
相关推荐
卜及中3 小时前
【Redis/2】核心特性、应用场景与安装配置
数据库·redis·缓存
LucianaiB3 小时前
如何做好一份优秀的技术文档:专业指南与最佳实践
android·java·数据库
Eiceblue3 小时前
Python读取PDF:文本、图片与文档属性
数据库·python·pdf
在未来等你6 小时前
SQL进阶之旅 Day 21:临时表与内存表应用
sql·mysql·postgresql·database·temporary-table·memory-table·sql-optimization
敖云岚6 小时前
【Redis】分布式锁的介绍与演进之路
数据库·redis·分布式
LUCIAZZZ7 小时前
HikariCP数据库连接池原理解析
java·jvm·数据库·spring·springboot·线程池·连接池
我在北京coding7 小时前
300道GaussDB(WMS)题目及答案。
数据库·gaussdb
小Tomkk7 小时前
阿里云 RDS mysql 5.7 怎么 添加白名单 并链接数据库
数据库·mysql·阿里云
明月醉窗台8 小时前
qt使用笔记二:main.cpp详解
数据库·笔记·qt
沉到海底去吧Go9 小时前
【图片自动识别改名】识别图片中的文字并批量改名的工具,根据文字对图片批量改名,基于QT和腾讯OCR识别的实现方案
数据库·qt·ocr·图片识别自动改名·图片区域识别改名·pdf识别改名