You can use OceanBase Connector/C to create connections between C-based applications and OceanBase Database. This topic describes the prerequisites and procedure for connecting to OceanBase Database by using OBCI.
Prerequisites
Before you install OceanBase Connector/C, make sure that you have set up the basic database development environment that meets the following requirements:
The GNU Compiler Collection (GCC) version is 3.4.6 or later. Version 4.8.5 is recommended.
The CMake version is 2.8.12 or later.
Contact OceanBase Technical Support to obtain the installation package of OceanBase Connector/C, also known as
LibOBClient.
Installation on Linux
Install
LibOBClient.sudo rpm -ivh libobclient-xx.x86_64.rpmInstall
OBClient.sudo rpm -ivh obclient-xx.x86_64.rpmNoteOBClientdepends onLibOBClient. Therefore, installLibOBClientfirst.
Compilation from the source code
The GitHub repository of OceanBase Connector/C provides the latest development version of the source code.
Install dependent tools.
sudo yum install -y git cmake gcc make openssl-devel ncurses-devel rpm-build gcc-c++ bison bison-devel zlib-devel gnutls-devel libxml2-devel openssl-devel \ libevent-devel libaio-develDownload the source code of
OBClient.cd rpmExecute the compilation script and download the code of
LibOBClient.sh build.sh # Compilation code cd libmariadb/rpm # Go to the package directory of LibOBClient.Build the RPM installation packages of
LibOBClientandOBClient.sh libobclient-build.sh # Build the installation package of LibOBClient. cd ../../rpm # Go to the package directory of OBClient. sh obclient-build.sh # Build the installation package of OBClient.Install
LibOBClient.sudo rpm -ivh libobclient-xx.x86_64.rpmInstall
OBClient.sudo rpm -ivh obclient-xx.x86_64.rpmNoteOBClientdepends onLibOBClient. Therefore, installLibOBClientfirst.
Sample code
The following sample code shows how to connect a C application to OceanBase Database:
mysql_library_init(0, NULL, NULL);
MYSQL *mysql = mysql_init(NULL);
/* Use the CLIENT_MULTI_STATEMENTS option to connect to the server. */
if (mysql_real_connect (mysql, host_name, user_name, password,
db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS) == NULL)
{
printf("mysql_real_connect() failed\n");
mysql_close(mysql);
mysql_library_end();
exit(1);
}
/* Execute multiple statements. */
status = mysql_query(mysql,
"DROP TABLE IF EXISTS tbl1;\
CREATE TABLE tbl1(id INT);\
INSERT INTO tbl1 VALUES(110);\
UPDATE tbl1 SET id=120 WHERE id=110;\
SELECT * FROM tbl1;\
DROP TABLE tbl1");
if (status)
{
printf("Could not execute statement(s)");
mysql_close(mysql);
mysql_library_end();
exit(0);
}
/* Process the result of each statement. */
do {
/* Check whether the current statement returns data. */
result = mysql_store_result(mysql);
if (result)
{
/* If yes, process the rows and release the result set. */
process_result_set(mysql, result);
mysql_free_result(result);
}
else /* No result set generated or an error occurred. */
{
if (mysql_field_count(mysql) == 0)
{
printf("%lld rows affected\n",
mysql_affected_rows(mysql));
}
else /* An error occurred. */
{
printf("Could not retrieve result set\n");
break;
}
}
/* Check whether more results are generated. The value -1 indicates no. The value 0 indicates yes, which means the loop continues. A value greater than 0 indicates that an error occurred. */
if ((status = mysql_next_result(mysql)) > 0)
printf("Could not execute statement\n");
} while (status == 0);
mysql_close(mysql);
mysql_library_end();For more information about OceanBase Connector/C, see OceanBase Connector/C Developer Guide.