注意:这里只是示例,操作过程中应该加以判断是否进行回滚操作
第一种:通过sql语句方法
//前面mysql初始化连接操作省略
mysql_query(&m_sqlCon, "BEGIN");//开启事务
mysql_query(&m_sqlCon, "SET NAMES 'GB2312'");//防止乱码
char *mysqlstatements = "INSERT INTO statics values(5, '张无忌', 88,0);";
mysql_real_query(&m_sqlCon, mysqlstatements, (unsigned long)strlen(mysqlstatements));//插入数据
mysql_query(&m_sqlCon, "COMMIT");//提交事务
//后续关闭连接操作省略
第二种方法:提供的函数
//前面mysql初始化连接操作省略
mysql_autocommit(&m_sqlCon,false);//关闭自动提交
mysql_query(&m_sqlCon, "SET NAMES 'GB2312'");//防止乱码
char *mysqlstatements = "INSERT INTO statics values(5, '张无忌', 88,0);";
mysql_real_query(&m_sqlCon, mysqlstatements, (unsigned long)strlen(mysqlstatements));//插入数据
mysql_commit(&m_sqlCon);
//失败应该用下面的回滚操作
//mysql_rollback(&m_sqlCon);
mysql_autocommit(&m_sqlCon,true);//恢复自动提交功能
//后续关闭连接操作省略
第三种方法:第二种方法的sql语句
ql初始化连接操作省略
mysql_query(&m_sqlCon, "SET AUTOCOMMIT=0");//关闭自动提交事务
mysql_query(&m_sqlCon, "SET NAMES 'GB2312'");//防止乱码
char *mysqlstatements = "INSERT INTO statics values(5, '张无忌', 88,0);";
mysql_real_query(&m_sqlCon, mysqlstatements, (unsigned long)strlen(mysqlstatements));//插入数据
mysql_query(&m_sqlCon, "COMMIT");//提交事务
//失败应该用回滚操作
//mysql_query(&m_sqlCon, "ROLLBACK");//提交事务
mysql_query(&m_sqlCon, "SET AUTOCOMMIT=1");//恢复自动提交事务
//后续关闭连接操作省略