All Products
Search
Document Center

Lindorm:Use C API to develop applications

Last Updated:Feb 29, 2024

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 the LindormTable endpoint for MySQL, 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 a local server and needs to connect to the Lindorm instance over the Internet, you can perform the following steps to enable the Internet endpoint for the instance in the Lindorm console: In the left-side navigation pane, select Database Connections > Wide Table Engine. On the Wide Table Engine tab, click Enable Public Endpoint.

    • If you use a VPC to access the Lindorm instance, specify the LindormTable VPC endpoint for MySQL in the value of lindorm_addr. If you use the Internet to access the Lindorm instance, specify the LindormTable Internet endpoint for MySQL in the value of lindorm_addr.

    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 by using MySQL. The value of this parameter is fixed to 33060.

  4. Establish a connection and use LindormTable SQL to perform operations in LindormTable. The following code block provides an example on how to use LindormTable SQL to create a table:

        // Establish 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. If the code is saved in the demo.c file, run the following command to compile the code:

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

Sample code

The following code provides a complete example on how to connect to and use LindormTable by using C API for MySQL:

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

    // Configure connection parameters.
    char lindorm_addr[] = "ld-uf6k8yqb741t3****-proxy-sql-lindorm-public.lindorm.rds.aliyuncs.com"; // Specify the LindormTable endpoint for MySQL.
    char lindorm_user[] = "user"; // Specify the username used to connect to LindormTable.
    char lindorm_password[] = "test"; // Specify the password used to connect to LindormTable.
    char database[] = "default"; // Specify the database that you want to connect.
    int lindorm_mysql_port = 33060; // Specify the port used to access LindormTable by using MySQL. The value of this parameter is fixed to 33060.

    // Establish 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 to the table.
    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 in the table.
    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