This topic describes how to configure and use OceanBase Connector/C, a C- and C++-based OceanBase Database client development component, and the MySQL C API, an official MySQL client development component.
We recommend that you use OceanBase Connector/C.
If you want to connect to MySQL tenants, you can use the MySQL C API.
Use the development components
The mysqlclient library stores C APIs. If you use GNU Compiler Collection (GCC) to write the code, specify header files in the -I option, library file directories in the -L option, and library names in the -l option. Sample statement:
OceanBase Connector/C
gcc test.c -I/u01/obclient/include -L/u01/obclient/lib -lobclntMySQL C API
gcc test.c -I/usr/include/mysql/ -L/usr/lib64/mysql -lmysqlclient
Notice
A library name is the corresponding library file name without "lib" at the beginning and
.so.xxat the end.
The following code shows how to query library file names:
[jayhart.xj@OceanBase008059.gtjsqa /usr]
$ ls -l /usr/lib64/mysql/ |grep mysqlclient
lrwxrwxrwx 1 root root 17 Dec 13 2015 libmysqlclient_r.so -> libmysqlclient.so
lrwxrwxrwx 1 root root 20 Dec 13 2015 libmysqlclient.so -> libmysqlclient.so.18
lrwxrwxrwx 1 root root 24 Dec 13 2015 libmysqlclient.so.18 -> libmysqlclient.so.18.0.0
-rwxrwxrwx 1 root root 3133560 Dec 13 2015 libmysqlclient_r.so.18.0.0
Sample code:
#include <mysql.h>
#include <stdio.h>
#include <string.h>
void main(void) {
MYSQL conn;
char server = "xxx.xxx.xxx.xxx";
char user = "root@test#obtest"; // The username in the format of username@tenant_name#cluster_name.
char password = "test"; // The password.
char *database = "test"; // The database name.
char str_sqls;
int status;
int result;
int i;
conn = mysql_init(NULL); / Connect to database /
/ connect to server with the CLIENT_MULTI_STATEMENTS option /
if (mysql_real_connect (conn, server, user, password,
database, 3306, NULL, CLIENT_MULTI_STATEMENTS) == NULL)
{
printf("mysql_real_connect() failed\n");
mysql_close(conn);
exit(1);
}
/ execute multiple statements /
strcat(str_sqls, "DROP TABLE IF EXISTS test_table;");
strcat(str_sqls, "CREATE TABLE test_table(id BIGINT);");
strcat(str_sqls, "INSERT INTO test_table VALUES(10);");
status = mysql_query(conn, str_sqls);
if (status)
{
printf("Could not execute statement(s)");
mysql_close(conn);
exit(0);
}
mysql_close(conn);
}
More information
For more information about OceanBase Connector/C, see OceanBase Connector/C.