DDL with MariaDB Connector/C
MariaDB Connector/C supports DDL operations such as CREATE TABLE, ALTER TABLE, and TRUNCATE TABLE, letting C applications modify database schema by executing SQL statements with mysql_query().
DDL Operations
Code Example: ALTER TABLE
#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>
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, // Connection
"mariadb.example.net", // Host
"db_user", // User account
"db_user_password", // User password
"test", // Default database
3306, // Port number
NULL, // Path to socket file
0 // Additional options
))
{
fprintf(stderr, "Error connecting to Server: %s\n", mysql_error(conn));
mysql_close(conn);
exit(1);
}
// Execute the ALTER TABLE statement
if (mysql_query(conn, "ALTER TABLE test.contacts RENAME COLUMN first_name TO f_name"))
{
fprintf(stderr, "Error altering table: %s\n", mysql_error(conn));
mysql_close(conn);
exit(1);
}
// Close the Connection
mysql_close(conn);
return 0;
}Code Example: TRUNCATE TABLE
Last updated
Was this helpful?

