You can use OceanBase Connector/C to create connections between C-based applications and OceanBase Database. This topic describes the prerequisites and procedure of 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.
Install Linux
Install
LibobClient.sudo rpm -ivh libobclient-xx.x86_64.rpmInstall
OBClient.sudo rpm -ivh obclient-xx.x86_64.rpmNote
OBClientdepends onLibobClient. Therefore, you must installLibobClientfirst.
Use the source code to compile a C application
In the GitHub repository of OceanBase Connector/C, you can find the source code of the latest version.
Install the dependency tool.
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 and libobclient (OceanBase Connector/C) from GitHub at:
obclient: https://github.com/oceanbase/obclientlibobclient: https://github.com/oceanbase/obconnector-c
Execute the compilation script to compile the source code. When you compile the code, the code of the submodule and OceanBase Connector/C is downloaded.
sh build.sh # Compile the code.Package up
libobclientinto an RPM package, which contains the SO and header files.cd libmariadb/rpm # Go to the package directory of libobclient. sh libobclient-build.sh # Package up libobclient into an installation package.Package up
obclientinto an RPM package.cd ../../rpm # Go to the package directory of OBClient. sh obclient-build.sh # Package up OBClient into an installation package.Install
LibobClient.sudo rpm -ivh libobclient-xx.x86_64.rpmInstall
OBClient.sudo rpm -ivh obclient-xx.x86_64.rpmNote
OBClientdepends onLibobClient. Therefore, you must 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 how to use OceanBase Connector/C, see OceanBase Connector/C.