5

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.

2
  • Does someone needs me to give a bigger snipet of code? I would gladly do that. Can you help me? Thanks. Commented Oct 11, 2013 at 10:22
  • Does this answer your question? MySQL Batch Updates in C Commented Oct 23, 2023 at 11:56

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. I will refer to C API support for extended inquiries. Kind regards.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.