MySQL C connector Multiple SQL statement execution example

Source: Internet
Author: User

Suppose the parameter SQL already contains more than one SQL statement. such as 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 autocommitif (! Autocommit (autocommit_disable, error)) {return false;} res = Mysql_real_query (M_mysql, Sql.c_str (), sql.length ());/if query FailIf (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_row S (m_mysql));} else {/* Some error occurred */printf ("Could not retrieve result set\n"); /* More results? -1 = no, >0 = error, 0 = yes (keep looping) */if ((res = Mysql_next_result (m_mysql)) > 0) printf ("Could not execute S Tatement\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 Multiple SQL statement execution example

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.