OceanBase logo

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Resources

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS

OceanBase Cloud

OceanBase Database

Tools

Connectors and Middleware

QUICK START

OceanBase Cloud

OceanBase Database

BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Company

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

International - English
中国站 - 简体中文
日本 - 日本語
Sign In
Start on Cloud

A unified distributed database ready for your transactional, analytical, and AI workloads.

DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS
OceanBase CloudOceanBase Database
ToolsConnectors and Middleware
QUICK START
OceanBase CloudOceanBase Database
BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

Start on Cloud
编组
All Products
    • Databases
    • iconOceanBase Database
    • iconOceanBase Cloud
    • iconOceanBase Tugraph
    • iconInteractive Tutorials
    • iconOceanBase Best Practices
    • Tools
    • iconOceanBase Cloud Platform
    • iconOceanBase Migration Service
    • iconOceanBase Developer Center
    • iconOceanBase Migration Assessment
    • iconOceanBase Admin Tool
    • iconOceanBase Loader and Dumper
    • iconOceanBase Deployer
    • iconKubernetes operator for OceanBase
    • iconOceanBase Diagnostic Tool
    • iconOceanBase Binlog Service
    • Connectors and Middleware
    • iconOceanBase Database Proxy
    • iconEmbedded SQL in C for OceanBase
    • iconOceanBase Call Interface
    • iconOceanBase Connector/C
    • iconOceanBase Connector/J
    • iconOceanBase Connector/ODBC
    • iconOceanBase Connector/NET
icon

OceanBase Database

SQL - V3.1.4Community Edition

  • Get Started
    • Overview
    • Quick Start
    • Hands on for OceanBase SQL
      • Before You Start
      • Basic Operations
    • Build Applications and Connect to OceanBase
      • Connect a Python application to OceanBase Database
      • Connect an ODBC application to OceanBase Database (using Unix ODBC)
      • Connect a Java application to OceanBase Database
      • Connect a Golang application to OceanBase Database (using Go-SQL-Driver/MySQL)
      • Connect a C application to OceanBase Database
    • Experience OceanBase Advanced Features
      • Experience Scalable OLTP
        • Run the TPC-C benchmark test in OceanBase Database
        • Experience the hot row update capability of OceanBase Database
      • Experience operational OLAP
      • Experience parallel import and data compression
      • Experience the multi-tenant feature
    • FAQ
    • Glossary

Download PDF

Overview Quick Start Before You Start Basic Operations Connect a Python application to OceanBase Database Connect an ODBC application to OceanBase Database (using Unix ODBC) Connect a Java application to OceanBase Database Connect a Golang application to OceanBase Database (using Go-SQL-Driver/MySQL) Connect a C application to OceanBase Database Experience operational OLAP Experience parallel import and data compression Experience the multi-tenant feature FAQ Glossary
OceanBase logo

The Unified Distributed Database for the AI Era.

Follow Us
Products
OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
Resources
DocsBlogLive DemosTraining & Certification
Company
About OceanBaseTrust CenterLegalPartnerContact Us
Follow Us

© OceanBase 2026. All rights reserved

Cloud Service AgreementPrivacy PolicySecurity
Contact Us
Document Feedback
  1. Documentation Center
  2. OceanBase Database
  3. SQL
  4. V3.1.4
iconOceanBase Database
SQL - V 3.1.4Community Edition
SQL
KV
  • V 4.4.2
  • V 4.3.5
  • V 4.3.3
  • V 4.3.1
  • V 4.3.0
  • V 4.2.5
  • V 4.2.2
  • V 4.2.1
  • V 4.2.0
  • V 4.1.0
  • V 4.0.0
  • V 3.1.4 and earlier
Community Edition
  • V 3.1.4

Connect a C application to OceanBase Database

Last Updated:2023-07-21 09:10:58  Updated
share
What is on this page
Prerequisites
Install the C driver
Install Linux
Compile from source code
Operation example
Sample code
Parameter description

folded

share

Connect a C application to OceanBase Database

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

  1. 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.

  2. Install libobclient.

    Note: Since obclient depends on libobclient, you need to install libobclient first.

    sudo rpm -ivh libobclient-xx.x86_64.rpm
    
  3. Install 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).

  1. 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-devel
    
  2. Code compilation and packaging

    1. Download the obclient source code (including libobclient).

      git clone --recurse-submodules https://github.com/oceanbase/obclient #Download obclient source code, including libobclient
      cd obclient
      
    2. Complie the libobclient code.

      cd obclient/libmariadb
      sh build.sh
      
    3. Package the RPM installation packages for libobclient.

      cd rpm
      sh libobclient-build.sh
      
    4. Complie the obclient code.

      cd ../..  #Back to the obclient directory
      sh build.sh
      
    5. Package the RPM installation packages for obclient.

      cd rpm
      sh obclient-build.sh
      
  3. 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.rpm
    

    Note: 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:

  1. Call mysql_library_init() to initialize the MySQL library.

    mysql_library_init(0, NULL, NULL);
    
  2. Call mysql_init() to initialize a connection handle.

    MYSQL *mysql = mysql_init(NULL);
    
  3. 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)
    
  4. Call mysql_real_query() or mysql_query() to send an SQL statement to the OBServer.

    mysql_query(mysql,"sql_statement");
    
  5. Call mysql_store_result() or mysql_use_result() to process its results.

    result=mysql_store_result(mysql);
    
  6. Call mysql_free_result() to release the memory.

    mysql_free_result(result);
    
  7. Call mysql_close() to close the connection to the OBServer.

    mysql_close(mysql);
    
  8. 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 root by 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.

Previous topic

Connect a Golang application to OceanBase Database (using Go-SQL-Driver/MySQL)
Last

Next topic

Run the TPC-C benchmark test in OceanBase Database
Next
What is on this page
Prerequisites
Install the C driver
Install Linux
Compile from source code
Operation example
Sample code
Parameter description