c语言mysql数据库事务开始、提交、回滚范例_MySQL
memset ( sql, 0x00, sizeof( sql ) );
memcpy ( sql, "set autocommit=0;", 17 );
if( mysql_query( sock, sql ) ){
sprintf( g_acTrcMsg, "关闭自动提交模式失败[%d][%s]", mysql_errno( sock ), mysql_error( sock ) );TRCLOG1
return -1;
}
2、 事务开始
memset ( sql, 0x00, sizeof( sql ) );
memcpy ( sql, "start transaction;", 18 );
if( mysql_query( sock, sql ) ){
sprintf( g_acTrcMsg, "建立事务失败[%d][%s]", mysql_errno( sock ), mysql_error( sock ) );TRCLOG1
return -1;
}
3、 事务回滚
memset ( sql, 0x00, sizeof( sql ) );
memcpy ( sql, "rollback;", 9 );
if( mysql_query( sock, sql ) ){
sprintf( g_acTrcMsg, "事务回滚失败[%d][%s]", mysql_errno( sock ), mysql_error( sock ) );TRCLOG1
return -1;
}
4、 事务提交
memset ( sql, 0x00, sizeof( sql ) );
memcpy ( sql, "commit;", 7 );
if( mysql_query( sock, sql ) ){
sprintf( g_acTrcMsg, "提交事务失败[%d][%s]\n", mysql_errno( sock ), mysql_error( sock ) );TRCLOG1
return -1;
}