Search the MySQL manual:

9.1.8 C API Handling of Multiple Query Execution

From version 4.1, MySQL supports the execution of multiple statements specified in a single query string. To use this capability with a given connection, you must specify the CLIENT_MULTI_QUERIES option in the flags parameter of mysql_real_connect() when opening the connection.

By default, mysql_query() and mysql_real_query() return only the first query status and the subsequent queries status can be processed using mysql_more_results() and mysql_next_result().


/* Connect to server with option CLIENT_MULTI_QUERIES */
mysql_real_connect(..., CLIENT_MULTI_QUERIES);

/* Now execute multiple queries */
mysql_query(mysql,"DROP TABLE IF EXISTS test_table;\
                   CREATE TABLE test_table(id INT);\
                   INSERT INTO test_table VALUES(10);\
                   UPDATE test_table SET id=20 WHERE id=10;\
                   SELECT * FROM test_table;\
                   DROP TABLE test_table";
while (mysql_more_results(mysql))
{
  /* Process all results */
  mysql_next_result(mysql);
  ...
  printf("total affected rows: %lld", mysql_affected_rows(mysql));
  ...
  if ((result= mysql_store_result(mysql))
  {
    /* Returned a result set, process it */
  }
}

User Comments

Add your own comment.