This topic provides a code example on how to connect a C application to OceanBase Database.
Prerequisites
Before installing and using OceanBase Connector/C, make sure you have set up a basic database development environment. The requirements are as follows:
- The GCC version is 3.4.6 or later (recommended version: 4.8.5).
- The CMake version is 2.8.12 or later.
Install the C driver
Install Linux
Download the OceanBase Connector/C installation package (libobclient) and the obclient installation package.
Operating System Installation Package Download Address Anolis 7, CentOS 7, RedHat 7, Ubuntu 20.X, Debian 9.X el7 download address (Simplified Chinese) Anolis 8, CentOS 8, RedHat 8, Debian 10 el8 download address (Simplified Chinese) Note: It is recommended that you download the latest version of the installation package.
Install libobclient.
Note: Since obclient depends on libobclient, you need to install libobclient first.
sudo rpm -ivh libobclient-xx.x86_64.rpmInstall obclient.
sudo rpm -ivh obclient-xx.x86_64.rpm
Compile from source code
OceanBase's GitHub repository provides the source code for the latest development version of obclient (including libobclient).
Install dependencies.
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-develCode compilation and packaging
Download the obclient source code (including libobclient).
git clone --recurse-submodules https://github.com/oceanbase/obclient #Download obclient source code, including libobclient cd obclientComplie the libobclient code.
cd obclient/libmariadb sh build.shPackage the RPM installation packages for libobclient.
cd rpm sh libobclient-build.shComplie the obclient code.
cd ../.. #Back to the obclient directory sh build.shPackage the RPM installation packages for obclient.
cd rpm sh obclient-build.sh
Install libobclient and obclient.
cd /obclient/libmariadb/rpm sudo rpm -ivh libobclient-xx.x86_64.rpm cd ../../rpm # obclient/rpm directory sudo rpm -ivh obclient-xx.x86_64.rpmNote: Since obclient depends on libobclient, you need to install libobclient first.
Operation example
To connect a C application to OceanBase Database through OceanBase Connector/C, perform the following steps:
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 its results.result=mysql_store_result(mysql);Call
mysql_free_result()to release the memory.mysql_free_result(result);Call
mysql_close()to close the connection to the OBServer.mysql_close(mysql);Call
mysql_library_end()to end the use of libobclient.mysql_library_end();
Sample code
The mysql_test.c file is used as an example, with the following code:
#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 = "***1**"; //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);
}
Parameter description
- hostname: IP address to connect to OceanBase Database, usually the OBProxy address.
- port: Port to connect to OceanBase Database, also the listening port of OBProxy. The default port is 2883, which can be customized.
- dbname: Name of the database to be accessed.
- username: The tenant's connection account, and the tenant's administrator username is
rootby default. - password: Account password.
After the code is edited, it can be compiled and executed with the following commands:
//Compile the code
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
//Execute the code
./ mysql_test
Note: The default path for obclient installation is /u01/obclient.