赞
踩
// 假设参数 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;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。