All Products
Search
Document Center

Lindorm:Use C API to develop applications

Last Updated:Feb 03, 2026

This topic describes how to use C API for MySQL to develop C or C++ applications.

Prerequisites

Procedure

  1. Install the dependency of C API for MySQL. The following command provides an example on how to install the dependency of C API for MySQL in CentOS:

  2. yum install mysql-devel
  3. Configure connection parameters.

    char lindorm_addr[] = "ld-uf6k8yqb741t3****-proxy-sql-lindorm-public.lindorm.rds.aliyuncs.com";
    char lindorm_user[] = "user";
    char lindorm_password[] = "test";
    char database[] = "default";
    int lindorm_mysql_port = 33060;

    Parameters

    Parameter

    Description

    lindorm_addr

    The LindormTable endpoint for MySQL. For more information about how to obtain it, see View endpoints.

    Important
    • If your application is deployed on an ECS instance, we recommend that you use a VPC to connect to the Lindorm instance to ensure higher security and lower network latency.

    • If your application is deployed on-premises, enable public network access in the console before connecting to the Lindorm instance over the public network. To do this, in the console, choose Database Connections > Wide Table Engine. On the Wide Table Engine tab, click Enable Public Endpoint.

    • To access a Lindorm instance from a virtual private cloud (VPC), set lindorm_addr to the MySQL-compatible VPC address. To access a Lindorm instance from the public network, set lindorm_addr to the MySQL-compatible Internet address.

    lindorm_user

    If you forget your password, you can change the password in the cluster management system of LindormTable. For more information, see Manage users.

    lindorm_password

    database

    The name of the database to which you want to connect. By default, your client is connected to a database named default.

    lindorm_mysql_port

    The port used to access LindormTable using MySQL. The value of this parameter is fixed to 33060.

  4. You can create a connection to LindormTable to use the wide table SQL syntax. The following example shows how to create a table.

        // Create a connection.
        if (!mysql_real_connect(&conn,lindorm_addr,lindorm_user,lindorm_password,database,lindorm_mysql_port,NULL,0)) {
            printf("Failed to connect to database: Error: %s\n",
                    mysql_error(&conn));
            exit(1);
        } else {
            printf("conect lindorm successfully\n");
        }
    
    
        // Create a table.
        char create_table[] = "create table if not exists user_test(id int, name varchar,age int, primary key(id))";
        res = mysql_query(&conn, create_table);
        if (!res) {
            printf("create table successfully\n");
        } else {
            printf("create table Error: %s\n", mysql_error(&conn));
            exit(1);
        }
  5. To compile the code in the demo.c file, execute the following command.

    gcc -o a.out $(mysql_config --cflags) demo.c $(mysql_config --libs)
    ./a.out

Complete example

The following shows the complete sample code:

#include <stdio.h>
#include "mysql/mysql.h"
int main(){
    MYSQL conn;
    int res;
    MYSQL_RES * result;
    MYSQL_ROW row;
    mysql_init(&conn);

    // Connection configuration.
    char lindorm_addr[] = "ld-uf6k8yqb741t3****-proxy-sql-lindorm-public.lindorm.rds.aliyuncs.com"; // LindormTable endpoint for MySQL
    char lindorm_user[] = "user"; // Username for LindormTable
    char lindorm_password[] = "test"; // Password for LindormTable
    char database[] = "default"; // Database name to connect to
    int lindorm_mysql_port = 33060; // MySQL protocol port for LindormTable (fixed at 33060)

    // Create a connection.
    if (!mysql_real_connect(&conn,lindorm_addr,lindorm_user,lindorm_password,database,lindorm_mysql_port,NULL,0)) {
        printf("Failed to connect to database: Error: %s\n",
                mysql_error(&conn));
        exit(1);
    } else {
        printf("conect lindorm successfully\n");
    }


    // Create a table.
    char create_table[] = "create table if not exists user_test(id int, name varchar,age int, primary key(id))";
    res = mysql_query(&conn, create_table);
    if (!res) {
        printf("create table successfully\n");
    } else {
        printf("create table Error: %s\n", mysql_error(&conn));
        exit(1);
    }

    // Insert data.
    char insert_data[] = "upsert into user_test(id,name,age) values(3,'wangwu',23)";
    res = mysql_query(&conn, insert_data);
    if (!res) {
        printf("insert data successfully\n");
    } else {
        printf("insert data Error: %s\n", mysql_error(&conn));
        exit(1);
    }

    // Query data.
    char select_query[] = "select * from user_test";
    if (mysql_query(&conn, select_query) != 0) {
        printf("select Error: %s\n", mysql_error(&conn));
        exit(1);
    } else {
        if ((result = mysql_store_result(&conn)) == NULL) {
            printf("store result Error: %s\n", mysql_error(&conn));
            exit(1);
        }
        else {
            while ((row = mysql_fetch_row(result)) != NULL) {
                printf("name is %s , ", row[0]);
                printf("age is %s\n", row[1]);
            }
        }
    }

    return 0;
}

The following result is returned:

conect lindorm successfully
create table successfully
insert data successfully
name is 3 , age is wangwu