Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perform select and multiple inserts as transaction using C Connector?

I am using MySQL. I have a select statement followed by a number of insert statement done using the C Connector. I would like to know how to put them all under one transaction and finally commit then.

I have gone through the MySQL 5.0 Reference Manual and C API Function Overview it have this function mysql_commit()? I must have a start transaction (how to set this is it by just turning off the autocommit()) and finally only commit right?

like image 474
user837306 Avatar asked Oct 28 '25 19:10

user837306


1 Answers

As far as I understand, there is no mysql_starttransaction() or something similar; so you're stuck with something like:

mysql_autocommit(conn, 0); 
//Do stuff here
mysql_commit(conn); //...or mysql_rollback(conn);

I would rather use the "query" method for all these calls:

mysql_query(conn, "START TRANSACTION");
//Do stuff here
mysql_query(conn, "COMMIT"); //...or mysql_query(conn, "ROLLBACK"); 

Also see this documentation.

like image 178
RobIII Avatar answered Oct 30 '25 10:10

RobIII



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!