START TRANSACTION
set autocommit=0
sql语句
出错 回滚 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;
}