Transactions with MariaDB Connector/C
MariaDB Connector/C supports multi-statement transactions with manual commit and rollback by disabling auto-commit with mysql_autocommit() on the connection.
Last updated
Was this helpful?
Was this helpful?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql.h>
// Update a contact's email by first name using the prepared statement
static void update_contact(MYSQL_STMT *stmt, const char *email, const char *first_name)
{
MYSQL_BIND bind[2];
memset(bind, 0, sizeof(bind));
bind[0].buffer_type = MYSQL_TYPE_STRING;
bind[0].buffer = (char *) email;
bind[0].buffer_length = strlen(email);
bind[1].buffer_type = MYSQL_TYPE_STRING;
bind[1].buffer = (char *) first_name;
bind[1].buffer_length = strlen(first_name);
if (mysql_stmt_bind_param(stmt, bind) || mysql_stmt_execute(stmt))
fprintf(stderr, "Error updating contact: %s\n", mysql_stmt_error(stmt));
}
int main(int argc, char *argv[])
{
// Initialize Connection
MYSQL *conn;
if (!(conn = mysql_init(0)))
{
fprintf(stderr, "unable to initialize connection struct\n");
exit(1);
}
// Connect to the database
if (!mysql_real_connect(
conn, "mariadb.example.net", "db_user", "db_user_password",
"test", 3306, NULL, 0))
{
fprintf(stderr, "Error connecting to Server: %s\n", mysql_error(conn));
mysql_close(conn);
exit(1);
}
// Disable auto-commit to start a user-managed transaction
mysql_autocommit(conn, 0);
// Prepare the UPDATE statement once and reuse it
MYSQL_STMT *stmt = mysql_stmt_init(conn);
if (mysql_stmt_prepare(stmt,
"UPDATE test.contacts SET email=? WHERE first_name=?", -1))
{
fprintf(stderr, "Error preparing statement: %s\n", mysql_stmt_error(stmt));
exit(1);
}
// Apply several updates within the transaction
update_contact(stmt, "johnsmith@example.com", "John");
update_contact(stmt, "jonsmith@example.com", "Jon");
update_contact(stmt, "johnnysmith@example.com", "Johnny");
// Commit the transaction; roll back on failure
if (mysql_commit(conn))
{
fprintf(stderr, "Error committing transaction: %s\n", mysql_error(conn));
mysql_rollback(conn);
}
// Close the statement and the connection
mysql_stmt_close(stmt);
mysql_close(conn);
return 0;
}SELECT * FROM test.contacts;+----+------------+-----------+-------------------------+
| id | first_name | last_name | email |
+----+------------+-----------+-------------------------+
| 1 | John | Smith | johnsmith@example.com |
| 2 | Jon | Smith | jonsmith@example.com |
| 3 | Johnny | Smith | johnnysmith@example.com |
+----+------------+-----------+-------------------------+