How to start a MySQL transaction that will be passed to mysql_commit ()

I am writing a C ++ application that uses the MySQL C> API to connect to a database. The MySQL server version is 5.6.19-log.

I need to run multiple SQL UPDATE , INSERT and DELETE in the same transaction to make sure that all changes or changes are not applied.

I found in the docs functions mysql_commit() and mysql_rollback() that complete the transaction (commit it or roll it back), but I can not find the corresponding function in which it starts the transaction.

Is there such a function? Am I missing something?


I run the UPDATE , INSERT and DELETE using mysql_real_query() .

I assume that I should start the transaction by running the START TRANSACTION SQL statement using the same mysql_real_query() function. Then I would have to complete the transaction by executing the COMMIT SQL statement using the same mysql_real_query() function.

But then, what is the point of having the mysql_commit() and mysql_rollback() functions allocated in the API?

+6
source share
1 answer
 //connect to mysql server: MYSQL *mysql = mysql_init(NULL); mysql = mysql_real_connect(mysql, ......) //turn off auto_commit mysql_autocommit(mysql , false); 

OR

 //start a tranaction directly as follows mysql_real_query(mysql,"BEGIN"); //run your commands: mysql_real_query(mysql,"UPDATE..."); //commit your transaction mysql_real_query(mysql, "COMMIT"); 
0
source

All Articles