user2279546
user2279546

Reputation:

multiple queries with mysql_query in a c++ project

So, this will not work with mysql_query.

I am strictly working with c++ and I am not using php.

I want this double query to be executed so that I will always have a unique ID in a transaction system with concurrent users creating IDs.

mysql_query(connection, \
"INSERT INTO User() VALUES ();  SELECT LAST_INSERT_ID(); ");

It works in MySql DataBase perfectly, but I need to add it to Eclipse( I am using Ubuntu 12.04 LTS).

My application is quite big and I would not like to change to mysqli, if this is possible but if there is no other way it will be ok.

Can you help me with this? Thanks in advance.

Upvotes: 5

Views: 4012

Answers (1)

Martin J.
Martin J.

Reputation: 5118

According to the MySQL C API documentation:

MySQL ... also supports the execution of a string containing multiple statements separated by semicolon (“;”) characters. This capability is enabled by special options that are specified either when you connect to the server with mysql_real_connect() or after connecting by calling mysql_set_server_option().

And:

CLIENT_MULTI_STATEMENTS enables mysql_query() and mysql_real_query() to execute statement strings containing multiple statements separated by semicolons. This option also enables CLIENT_MULTI_RESULTS implicitly, so a flags argument of CLIENT_MULTI_STATEMENTS to mysql_real_connect() is equivalent to an argument of CLIENT_MULTI_STATEMENTS | CLIENT_MULTI_RESULTS. That is, CLIENT_MULTI_STATEMENTS is sufficient to enable multiple-statement execution and all multiple-result processing.

So, you can supply several statements in a single mysql_query() call, separated by a semicolon, assuming you set up your mysql connection a bit differently, using mysql_real_connect. You need to pass the following flag as the last argument: CLIENT_MULTI_STATEMENTS, whose documentation says:

Tell the server that the client may send multiple statements in a single string (separated by “;”). If this flag is not set, multiple-statement execution is disabled. See the note following this table for more information about this flag.

See C API Support for Multiple Statement Execution and mysql_real_connect() for more details.

Upvotes: 6

Related Questions