Connect to LindormTable from a C or C++ application using the Open Database Connectivity (ODBC) driver for MySQL.
Prerequisites
Before you begin, make sure you have:
MySQL compatibility enabled for your Lindorm instance. See Enable the MySQL compatibility feature.
Your client IP address added to the instance whitelist. See Configure whitelists.
Set up the driver
Step 1: Install the MySQL ODBC driver
Download and install the ODBC driver for MySQL from the MySQL official website.
Step 2: Install unixODBC
Install unixODBC on Linux:
yum install unixODBC-develStep 3: Configure the driver in odbcinst.ini
Add the following entry to your odbcinst.ini file.
[MySQL]
Description = ODBC for MySQL
Driver = /usr/lib/libmyodbc8a.so
Setup = /usr/lib/libmyodbc8w.so
Driver64 = /usr/lib64/libmyodbc8a.so
Setup64 = /usr/lib64/libmyodbc8w.so
FileUsage = 1| Parameter | Description |
|---|---|
Description | A label for this driver entry. Set a custom value. |
Driver | Path to the 32-bit ODBC driver library. |
Setup | Path to the 32-bit ODBC driver installer library. |
Driver64 | Path to the 64-bit ODBC driver library. |
Setup64 | Path to the 64-bit ODBC driver installer library. |
FileUsage | Fixed value. Do not change it. Value: 1. |
Step 4: Connect to LindormTable in code
Use SQLDriverConnect to establish a connection. The connection string format is:
DRIVER={MySQL};SERVER=<endpoint>;PORT=33060;DATABASE=<database>;USER=<username>;PASSWORD=<password>| Parameter | Description |
|---|---|
DRIVER | The driver name as configured in odbcinst.ini. |
SERVER | The LindormTable endpoint for MySQL. See View endpoints. |
PORT | The port for MySQL access to LindormTable. Fixed value: 33060. |
DATABASE | The database to connect to. By default, your client is connected to a database named default. |
USER | Your LindormTable username. To reset your password, see Manage users. |
PASSWORD | Your LindormTable password. |
Choosing the right endpoint for SERVER:
ECS instance (VPC): Use the LindormTable VPC endpoint for MySQL for lower latency and higher security.
Local server (Internet): Enable the public endpoint first in the Lindorm console — go to Database Connections > Wide Table Engine, then click Enable Public Endpoint on the Wide Table Engine tab. Then use the LindormTable Internet endpoint for MySQL.
Step 5: Run queries
After a successful connection, allocate a statement handle and execute SQL with SQLExecDirect. The following example runs show databases and prints each result row:
if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO) {
printf("Connection established. \n");
// Perform the query.
SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt);
SQLExecDirect(stmt, (SQLCHAR*)"show databases", SQL_NTS);
// Obtain query results.
SQLCHAR result[50];
while (SQLFetch(stmt) == SQL_SUCCESS) {
SQLGetData(stmt, 1, SQL_C_CHAR, result, sizeof(result), NULL);
printf("database: %s\n", result);
}
// Release resources.
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
SQLDisconnect(dbc);
} else {
SQLCHAR sqlState[6];
SQLCHAR msg[SQL_MAX_MESSAGE_LENGTH];
SQLINTEGER nativeError;
SQLSMALLINT actualMsgLen;
SQLGetDiagRec(SQL_HANDLE_DBC, dbc, 1, sqlState, &nativeError, msg, sizeof(msg), &actualMsgLen);
printf("Connection failed: %s\n", msg);
}
// Release resources.
SQLFreeHandle(SQL_HANDLE_DBC, dbc);
SQLFreeHandle(SQL_HANDLE_ENV, env);
return 0;Complete example
The following program initializes the ODBC environment, connects to LindormTable, queries all databases, and releases all resources.
Replace the placeholder values in the connection string with your actual endpoint, database name, username, and password.
#include <stdio.h>
#include <stdlib.h>
#include <sql.h>
#include <sqlext.h>
int main() {
// Define connection handles.
SQLHENV env;
SQLHDBC dbc;
SQLHSTMT stmt;
SQLRETURN ret;
// Initialize the ODBC environment.
SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, SQL_IS_INTEGER);
// Allocate a connection handle and set the login timeout.
SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);
SQLSetConnectAttr(dbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER)5, SQL_IS_INTEGER);
// Connect to LindormTable.
// Replace the SERVER, DATABASE, USER, and PASSWORD values with your own.
ret = SQLDriverConnect(dbc, NULL, (SQLCHAR*)"DRIVER={MySQL};SERVER=ld-uf6k8yqb741t3****-proxy-sql-lindorm-public.lindorm.rds.aliyuncs.com;PORT=33060;DATABASE=default;USER=user;PASSWORD=test", SQL_NTS, NULL, 0, NULL, SQL_DRIVER_COMPLETE);
if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO) {
printf("Connection established. \n");
// Run a query.
SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt);
SQLExecDirect(stmt, (SQLCHAR*)"show databases", SQL_NTS);
// Print each row.
SQLCHAR result[50];
while (SQLFetch(stmt) == SQL_SUCCESS) {
SQLGetData(stmt, 1, SQL_C_CHAR, result, sizeof(result), NULL);
printf("database: %s\n", result);
}
// Release statement resources.
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
SQLDisconnect(dbc);
} else {
SQLCHAR sqlState[6];
SQLCHAR msg[SQL_MAX_MESSAGE_LENGTH];
SQLINTEGER nativeError;
SQLSMALLINT actualMsgLen;
SQLGetDiagRec(SQL_HANDLE_DBC, dbc, 1, sqlState, &nativeError, msg, sizeof(msg), &actualMsgLen);
printf("Connection failed: %s\n", msg);
}
// Release connection and environment resources.
SQLFreeHandle(SQL_HANDLE_DBC, dbc);
SQLFreeHandle(SQL_HANDLE_ENV, env);
return 0;
}If your instance has only the default database, the output is:
Connection established.
database: default
database: information_schema