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;
	}
相关推荐
TDengine (老段)3 小时前
煤机设备每天 TB 级数据,天地奔牛用 TDengine 把查询提速到“秒级”
大数据·运维·数据库·struts·架构·时序数据库·tdengine
泯仲3 小时前
从零起步学习MySQL 第二章:DDL语句定义及常见用法示例
数据库·mysql
Leon-Ning Liu3 小时前
记录MySQL 主从架构切换双主(互为主从)操作步骤
数据库·mysql
@insist1233 小时前
数据库工程师核心 TCP/IP 协议栈知识:从软考考点到运维实战
运维·数据库·网络协议·tcp/ip·软考·数据库系统工程师·软件水平考试
!chen3 小时前
Oracle数据库物理备份工具支持本机+异机
数据库
前进的李工4 小时前
数据库视图:数据安全与权限管理利器
开发语言·数据库·mysql·navicat
what丶k4 小时前
深度解析 Canal 数据同步:原理、实操与生产级最佳实践
数据库·后端
白鲸开源4 小时前
(三)ODS/明细层落地设计要点:把数据接入层打造成“稳定可运维”的基础设施
大数据·数据结构·数据库
程序员这么可爱4 小时前
MySQL分页踩坑实录:LIMIT分页出现重复数据,同一主键ID跨页重复完美解决
数据库·mysql·limit分页重复·sql分页优化·数据库踩坑·主键排序规范
Elastic 中国社区官方博客5 小时前
需要知道某个同义词是否实际匹配了你的 Elasticsearch 查询吗?
大数据·数据库·elasticsearch·搜索引擎·全文检索