This topic describes how to connect C applications to OceanBase Database.
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 OceanBase Connector/C
Install Linux
Install
LibOBClient.sudo rpm -ivh libobclient-xx.x86_64.rpmInstall
OBClient.sudo rpm -ivh obclient-xx.x86_64.rpmNote
OBClientdepends onLibOBClient. Therefore, installLibOBClientfirst.
Compile a C application using source code
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.
cd rpmExecute the script and download the code of LibobClient.
sh build.sh # Compile the code. cd libmariadb/rpm # Access the package directory of LibobClient.Compress the programs of
LibobClientandOBClientin RPM packages.sh libobclient-build.sh # Compress LibobClient in a package. cd ../../rpm # Access the package directory of OBClient. sh obclient-build.sh # Compress the program of OBClient in a package.Install
LibobClient.sudo rpm -ivh libobclient-xx.x86_64.rpmInstall
OBClient.sudo rpm -ivh obclient-xx.x86_64.rpmNote
Because
OBClientdepends onLibobClient, installLibobClientfirst.
Procedure
The basic methods for an application to interact with an OBServer by using OceanBase Connector/C include:
Call
mysql_library_init()to initialize the MySQL library.mysql_library_init(0, NULL, NULL);Call
mysql_init()to initialize a connection handle.MYSQL *mysql = mysql_init(NULL);Call
mysql_real_connect()to connect to the OBServer.mysql_real_connect (mysql, host_name, user_name, password, db_name, port_num, socket_name, CLIENT_MULTI_STATEMENTS)Call
mysql_real_query()ormysql_query()to send an SQL statement to the OBServer.mysql_query(mysql,"sql_statement");Call
mysql_store_result()ormysql_use_result()to process the result.result=mysql_store_result(mysql);Call
mysql_free_result()to release the memory.mysql_free_result(result);Call
mysql_close()to disconnect from the OBServer.mysql_close(mysql);Call
mysql_library_end()to stop usingLibobClient.mysql_library_end();
For more information about OceanBase Connector/C, see OceanBase Connector/C.