码迷,mamicode.com
首页 > 数据库 > 详细

mysql c connector 多条sql语句执行示例

时间:2018-05-08 23:56:01      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:amp   ext   tab   参数   into   could not   length   mysql   val   

 

//  假设参数 sql已经包含多条sql语句。如 sql = "insert into table1(...) values(...); update table2 set a=1;";  

 

const bool CDBOperator::MultiQuery(const std::string sql, std::string& error)

{
int res = 0;
bool ret = false;
MYSQL_RES* result = NULL;
std::string e;
try
{
//disable autocommit
if(!AutoCommit(AUTOCOMMIT_DISABLE, error))
{
return false;
}
res = mysql_real_query(m_mysql, sql.c_str(), sql.length());
// if query fail
if(0 != res)
{
error = mysql_error(m_mysql);
Rollback(e);
AutoCommit(AUTOCOMMIT_ENABLE, e);
return false;
}
do
{
/* did current statement return data? */
result = mysql_store_result(m_mysql);
if (result) {
mysql_free_result(result);
} else { /* no result set or error */
if (mysql_field_count(m_mysql) == 0) {
printf("%lld rows affected\n",
mysql_affected_rows(m_mysql));
} else { /* some error occurred */
printf("Could not retrieve result set\n");
break;
}
}
/* more results? -1 = no, >0 = error, 0 = yes (keep looping) */
if ((res = mysql_next_result(m_mysql)) > 0)
printf("Could not execute statement\n");
} while (res == 0);
ret = Commit(error);
AutoCommit(AUTOCOMMIT_ENABLE, e);
if(!ret)
{
return false;
}
return true;
}
catch(...)
{
error = mysql_error(m_mysql);
return false;
}

mysql c connector 多条sql语句执行示例

标签:amp   ext   tab   参数   into   could not   length   mysql   val   

原文地址:https://www.cnblogs.com/qianlicao/p/9011559.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!