当前位置:   article > 正文

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

mysql-connector-c++-8.1.0 mysql执行sql语句

//  假设参数 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;

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/676520
推荐阅读
相关标签
  

闽ICP备14008679号