This topic describes how to connect a C application 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.
Install OceanBase Connector/C
Install Linux
Obtain the RPM packages of OBClient and OceanBase Connector/C, which is also known as libobclient.
You need to visit the image repository and find the installation packages based on your system version. For more information, see Software and hardware requirements.
Install
libobclient.sudo rpm -ivh libobclient-xx.x86_64.rpmInstall OBClient.
sudo rpm -ivh obclient-xx.x86_64.rpmNote
OBClient depends on
libobclient. Therefore, you must installlibobclientfirst.
Compile OBClient from source code
OceanBase Database provides the source code of the latest OBClient in its repository on Github, which also includes the repository of libobclient.
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-develCompile and package the code.
Download the source code of OBClient, which also includes sub-repositories.
git clone --recurse-submodules https://github.com/oceanbase/obclient #Download the source code of OBClient, which also includes sub-repositories. cd obclientCompile the script of
libobclient.cd obclient/libmariadb sh build.shCompress the
libobclientprogram in an RPM package.cd rpm sh libobclient-build.shCompile the script of OBClient.
cd ../.. # Return to the obclient directory sh build.shCompress the OBClient program in an RPM package.
cd rpm sh obclient-build.sh
Install
libobclientand OBClient.cd /obclient/libmariadb/rpm sudo rpm -ivh libobclient-xx.x86_64.rpm cd ../../rpm # Access the obclient/rpm directory. sudo rpm -ivh obclient-xx.x86_64.rpmNote
OBClient depends on
libobclient. Therefore, you must installlibobclientfirst.
Examples
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();
Sample code
The following sample code uses the mysql_test.c file as an example:
#include "mysql.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
mysql_library_init(0, NULL, NULL);
MYSQL *mysql = mysql_init(NULL);
char* host_name = "172.xx.xx.xx";//set your mysql host
char* user_name = "r***"; //set your user_name
char* password = "******"; //set your password
char* db_name = "test"; //set your databasename
int port_num = 2883; //set your mysql port
char* socket_name = NULL;
MYSQL_RES* result;
int status = 0;
/* connect to server with the CLIENT_MULTI_STATEMENTS option */
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);
exit(1);
}
/* execute multiple statements */
status = mysql_query(mysql,
"DROP TABLE IF EXISTS test_table;\
CREATE TABLE test_table(id INT);\
INSERT INTO test_table VALUES(10);\
UPDATE test_table SET id=20 WHERE id=10;\
SELECT * FROM test_table;\
DROP TABLE test_table");
if (status)
{
printf("Could not execute statement(s)");
mysql_close(mysql);
exit(0);
}
/* process each statement result */
do {
/* did current statement return data? */
result = mysql_store_result(mysql);
if (result)
{
/* yes; process rows and free the result set */
//process_result_set(mysql, result);
mysql_free_result(result);
}
else /* no result set or error */
{
if (mysql_field_count(mysql) == 0)
{
printf("%lld rows affected\n",
mysql_affected_rows(mysql));
}
else /* some error occurred */
{
printf("Could not retrieve result set\n");
break;
}
}
/* more results? -1 = no, >0 = error, 0 = yes (keep looping) */
if ((status = mysql_next_result(mysql)) > 0)
printf("Could not execute statement\n");
} while (status == 0);
mysql_close(mysql);
}
Parameters
hostname: the IP address for connecting to OceanBase Database, which is usually the IP address of an OBProxy.
username: the connection account of the tenant. By default, the username of the tenant administrator is
root.password: the password of the connection account.
dbname: the name of the database to be connected.
port: the port for connecting to OceanBase Database, which is also the listening port of the OBProxy. Default value: 2883, which can be customized.
After you edit the code, run the following command to compile and execute the file:
//Compile the file.
g++ -I/u01/obclient/include/ -L/u01/obclient/lib -lobclnt mysql_test.c -o mysql_test
//Specify the execution path.
export LD_LIBRARY_PATH=/u01/obclient/lib
//Run the file.
./mysql_test
Note
By default, OBClient is installed in the
/u01/obclientdirectory.