DDL with MariaDB Connector/C++
This page is part of MariaDB's Documentation.
The parent of this page is: MariaDB Connector/C++
Topics on this page:
Overview
C++ developers can use MariaDB Connector/C++ to perform basic DDL (Data Definition Language) operations with MariaDB database products.
DDL Operations
DDL (Data Definition Language) refers to all SQL-schema statements in the SQL standard (ISO/IEC 9075-2:2016).
Some examples of DDL include
, , , , and .Code Example: ALTER TABLE
is a DDL (Data Definition Language) operation that changes an existing table.
The following code demonstrates how to execute example table:
on the// Includes
#include <iostream>
#include <mariadb/conncpp.hpp>
// Function to Alter Table
void alterTable(std::shared_ptr<sql::Statement> &stmnt)
{
try {
// Alter contacts Table
stmnt->executeUpdate("ALTER TABLE test.contacts RENAME COLUMN first_name TO f_name");
}
// Catch Exception
catch (sql::SQLException& e) {
std::cerr << "Error altering table: "
<< e.what() << std::endl;
}
}
// Main Process
int main(int argc, char **argv)
{
try {
// Instantiate Driver
sql::Driver* driver = sql::mariadb::get_driver_instance();
// Configure Connection
// The URL or TCP connection string format is
// ``jdbc:mariadb://host:port/database``.
sql::SQLString url("jdbc:mariadb://192.0.2.1:3306/test");
// Use a properties map for the other connection options
sql::Properties properties({
{"user", "db_user"},
{"password", "db_user_password"},
});
// Establish Connection
// Use a smart pointer for extra safety
std::unique_ptr<sql::Connection> conn(driver->connect(url, properties));
// Create a Statement
// Use a smart pointer for extra safety
std::shared_ptr<sql::Statement> stmnt(conn->createStatement());
// Use Statement to alter table
alterTable(stmnt);
// Close Connection
conn->close();
}
// Catch Exceptions
catch (sql::SQLException& e) {
std::cerr << "Error Connecting to the database: "
<< e.what() << std::endl;
// Exit (Failed)
return 1;
}
// Exit (Success)
return 0;
}
Confirm the table was properly altered by using MariaDB Client to execute a statement on the same table:
DESC contacts;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| f_name | varchar(25) | YES | | NULL | |
| last_name | varchar(25) | YES | | NULL | |
| email | varchar(100) | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
Code Example: TRUNCATE TABLE
is a DDL (Data Definition Language) operation that deletes all data from an existing table.
The following code demonstrates how to execute example table:
on the// Includes
#include <iostream>
#include <mariadb/conncpp.hpp>
// Function to Truncate Table
void truncateTable(std::shared_ptr<sql::Statement> &stmnt)
{
try {
// TRUNCATE contacts Table
stmnt->executeUpdate("TRUNCATE test.contacts");
}
// Catch Exception
catch (sql::SQLException& e) {
std::cerr << "Error truncating table: "
<< e.what() << std::endl;
}
}
// Main Process
int main(int argc, char **argv)
{
try {
// Instantiate Driver
sql::Driver* driver = sql::mariadb::get_driver_instance();
// Configure Connection
// The URL or TCP connection string format is
// ``jdbc:mariadb://host:port/database``.
sql::SQLString url("jdbc:mariadb://192.0.2.1:3306/test");
// Use a properties map for the other connection options
sql::Properties properties({
{"user", "db_user"},
{"password", "db_user_password"},
});
// Establish Connection
// Use a smart pointer for extra safety
std::unique_ptr<sql::Connection> conn(driver->connect(url, properties));
// Create a Statement
// Use a smart pointer for extra safety
std::shared_ptr<sql::Statement> stmnt(conn->createStatement());
// Use Prepared Statement to truncate table
truncateTable(stmnt);
// Close Connection
conn->close();
}
// Catch Exceptions
catch (sql::SQLException& e) {
std::cerr << "Error Connecting to the database: "
<< e.what() << std::endl;
// Exit (Failed)
return 1;
}
// Exit (Success)
return 0;
}
The following query confirms that the example table:
statement deleted all rows from theSELECT * from test.contacts;
Empty set (0.000 sec)