MySQL Connector/C, also known as the MySQL C API, provides a set of C language functions and data structures that allow developers to connect to, query, and manage MySQL databases using C/C++ languages. This API enables developers to directly use MySQL databases within their C/C++ applications.
This topic describes how to build an application using the MySQL Connector/C (libmysqlclient) driver and OceanBase Cloud to perform basic operations such as creating tables, inserting data, and querying data.
Prerequisites
Before installing and using MySQL Connector/C (libmysqlclient), ensure you have set up a basic database development environment with the following requirements:
- GCC version 3.4.6 or later. Version 4.8.5 is recommended.
- CMake version 2.8.12 or later.
- You have registered an OceanBase Cloud account and created a cluster instance and a tenant in MySQL mode. For details, see Create a cluster instance and Create a tenant.
Procedure
- Obtain the connection information for OceanBase Cloud.
- Install the MySQL Connector/C driver.
- Write the application.
- Run the application.
Step 1: Obtain the database connection string
Log in to the OceanBase Cloud console. On the instance list page, expand the target instance's information, and under the target tenant, select Connect > Get connection string.
For more detailed operations, see Get a connection string.
Fill in the corresponding information in the URL below based on your created OceanBase Cloud.
obclient -h$host -P$port -u$user_name -p$password -D$database_nameParameter description:
$host: The connection address for OceanBase Cloud, for example,t********.********.oceanbase.cloud.$port: The connection port for OceanBase Cloud, which is 3306 by default.$database_name: The name of the database to access.Note
The user connecting to the tenant must have the
CREATE,INSERT,DROP, andSELECTprivileges on this database. For more information about account privileges, see Create and manage accounts.$user_name: The account used to access the database.$password: The account password.
Example:
obclient -h t********.********.oceanbase.cloud -P3306 -u mysql001 -p****** -Dtest
Step 2: Install the MySQL Connector/C driver
## To install on Ubuntu, use the following command:
sudo apt-get install libmysqlclient-dev
## To install on a CentOS system, use the following code:
sudo yum install mysql-devel
## Confirm whether the installation was successful
mysql_config --version
Step 3: Write an application
Open your text editor, edit the sample test.cc file, and save it. The code is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
int main() {
MYSQL *conn = mysql_init(NULL); // Initialize a MySQL connection.
if (conn == NULL) {
fprintf(stderr, "mysql_init() failed\n");
return 1;
}
// Connect to the MySQL database server.
if (mysql_real_connect(conn, "host", "user", "passwd", "db", port, NULL, 0) == NULL) {
fprintf(stderr, "mysql_real_connect() failed\n");
mysql_close(conn);
return 1;
}
// Create a user table.
if (mysql_query(conn, "CREATE TABLE IF NOT EXISTS users (id INT AUTO_INCREMENT, name VARCHAR(255), email VARCHAR(255), PRIMARY KEY(id))") != 0) {
fprintf(stderr, "Error in creating table: %s\n", mysql_error(conn));
mysql_close(conn);
return 1;
}
// Insert data.
if (mysql_query(conn, "INSERT INTO users (name, email) VALUES ('Xiao Ming', 'xiaoming@example.com')") != 0) {
fprintf(stderr, "Error in inserting data: %s\n", mysql_error(conn));
mysql_close(conn);
return 1;
}
// Query data
if (mysql_query(conn, "SELECT * FROM users") == 0) {
MYSQL_RES *result = mysql_store_result(conn);
if (result != NULL) {
int num_fields = mysql_num_fields(result);
MYSQL_ROW row;
while ((row = mysql_fetch_row(result))) {
for (int i = 0; i < num_fields; i++) {
printf("%s ", row[i] ? row[i] : "NULL");
}
printf("\n");
}
mysql_free_result(result);
} else {
fprintf(stderr, "Error in retrieving result: %s\n", mysql_error(conn));
}
} else {
fprintf(stderr, "Error in selecting data: %s\n", mysql_error(conn));
}
// Drop the table
if (mysql_query(conn, "DROP TABLE IF EXISTS users") != 0) {
fprintf(stderr, "Error in dropping table: %s\n", mysql_error(conn));
mysql_close(conn);
return 1;
}
mysql_close(conn); // Close the connection.
return 0;
}
Modify the database connection information in the test.cc project file based on the information obtained in Step 1: Obtain OceanBase Cloud connection information.
- In a Linux environment, you can use the
vi test.ccorvim test.cccommand to edit thetest.ccfile and modify the database connection information to match your actual configuration.
The following example shows the database connection information in the test.cc file:
# Modify this part of the connection information with the actual connection string obtained. Example:
if (mysql_real_connect(conn, "t********.********.oceanbase.cloud", "mysql001", "********", "test", 3306, NULL, 0) == NULL) {
fprintf(stderr, "mysql_real_connect() failed\n");
mysql_close(conn);
return 1;
}
Step 4: Compile the application
After editing the code, compile it using the following command.
gcc -o test test.cc `mysql_config --cflags --libs`
After successful compilation, an executable file named test is generated.
Step 5: Run the application
Run the application using the following command.
./test
The output result is as follows. If this output is displayed, it indicates a successful database connection and correct execution of the sample statement.
1 Xiao Ming xiaoming@example.com