Programming Reference/Librarys
Question & Answer
Q&A is closed
my_bool mysql_commit(MYSQL *mysql)
mysql_commit Commits the current transaction.
The action of this function is subject to the value of the completion_type system variable.
if the value of completion_type is 2, the server performs a release after terminating a transaction and closes the client connection. The client program should call mysql_close().
#include <stdio.h> /* including standard library */ #include <mysql/mysql.h> MYSQL *my; MYSQL_ROW row; MYSQL_RES *mysql_res; int main( void ){ char host[20]; char user[20]; char pass[20]; unsigned long numrows = 0; my = mysql_init(NULL); sprintf(host,"localhost"); sprintf(user,"username"); sprintf(pass,"password"); if (my == NULL ) { printf("Cant initalisize MySQL\n"); return 1; } if( mysql_real_connect (my,host,user,pass,NULL,0,NULL,0) == NULL) { printf("Error cant login\n"); } else { printf("Login correct\n"); } mysql_select_db(my,"test"); mysql_autocommit(my, (my_bool)0); // set autocommit to false mysql_query(my, "TRUNCATE TABLE `collection`;"); mysql_query(my, "INSERT INTO `test`.`collection`" " (`id`, `name`, `title`, `published`)" " VALUES (NULL, 'Debian', 'Debian 4.0', '2011-02-14');"); mysql_query(my, "SELECT * from collection WHERE 1;"); if (mysql_affected_rows(my) >= 1) { mysql_rollback(my); } else { mysql_commit(my); } printf("rows affected: %li\n", numrows); mysql_res = mysql_store_result(my); row = mysql_fetch_row (mysql_res); // fetch the result numrows = (unsigned long) mysql_num_rows (mysql_res); printf("rows affected after mysql_commit: %li\n", numrows); mysql_close(my); return 0; }
Login correct rows affected: 0 rows affected after mysql_commit: 1