Important
The SingleStore 9.0 release candidate (RC) gives you the opportunity to preview, evaluate, and provide feedback on new and upcoming features prior to their general availability. In the interim, SingleStore 8.9 is recommended for production workloads, which can later be upgraded to SingleStore 9.0.
Connect with C/C++
On this page
You can connect to your SingleStore database from C/C++ based applications using the MariaDB Connector (C/C++) (LGPLv2.
Note
As of MariaDB Connector/C++ version 1.rewriteBatchedStatements=true
may not work as expected.
Refer to MariaDB Connector/C and Connector/C++ for more information.
Examples
Update the connection configuration of your SingleStore deployment before running the code.
-
endpoint
: IP address or hostname of the SingleStore deployment. -
database
: Name of the SingleStore database to connect with. -
username
: Username of the SingleStore database user with which to connect to SingleStore. -
password
: Password of the SingleStore database user.
Example: Connect using C++
This example creates a table in SingleStore, inserts data into the table, and queries the data using C++.
Note
The following commands may require sudo
access (root
privileges).
-
Install the required dependencies.
This example uses a Debian-based Linux OS. apt-get install cmake libmariadb-dev libssl-dev -
Download and install the MariaDB C++ connector.
-
Clone the connector repository.
git clone https://github.com/mariadb-corporation/mariadb-connector-cpp.gitcd mariadb-connector-cpp -
Build and install the connector.
cmake .makemake installThe
mariadbapp
library is installed in the/usr/local/lib
directory. -
Add a symbolic link for
libmariadbcpp.
.so ln -s /usr/local/lib/mariadb/libmariadbcpp.so /usr/local/libldconfig
-
-
Add the following code to a s2example.
cpp file. Update the connection configuration of your SingleStore deployment in the code. #include <mariadb/conncpp.hpp>#include <iostream>int main() {try {// Connection detailsconst std::string url = "tcp://<endpoint>:3306/<database>";const std::string user = "<username>";const std::string password = "<password>";// Connectsql::Driver* driver = sql::mariadb::get_driver_instance();sql::SQLString connection_url(url);sql::Properties properties({{"user", user}, {"password", password}});std::unique_ptr<sql::Connection> conn(driver->connect(connection_url, properties));std::unique_ptr<sql::Statement> stmt(conn->createStatement());// Create operationstmt->execute("CREATE TABLE IF NOT EXISTS Stock (ID INT PRIMARY KEY,Code VARCHAR(10),Quantity INT)");// Insert operationstmt->execute("INSERT INTO Stock (ID, Code, Quantity) VALUES (3, 'cdq3', 25)");stmt->execute("INSERT INTO Stock (ID, Code, Quantity) VALUES (1, 'xvf1', 40)");stmt->execute("INSERT INTO Stock (ID, Code, Quantity) VALUES (2, 'gwl2', 15)");// Read operationstd::unique_ptr<sql::ResultSet> res(stmt->executeQuery("SELECT * FROM Stock ORDER BY ID"));// Display resultswhile (res->next()) {std::cout << "ID: " << res->getInt("ID")<< ", Code: " << res->getString("Code")<< ", Quantity: " << res->getInt("Quantity") << std::endl;}} catch (sql::SQLException &e) {std::cerr << "SQL Exception: " << e.what() << std::endl;return 1;}return 0;} -
Run the following command to compile the code.
Run this command in the same directory as s2example. cpp (or append the path to the file). g++ s2example.cpp -o singlestore_mariadb \-I/usr/local/include/mariadb \-L/usr/local/lib/mariadb \-lmariadbcpp -
Run the code.
Run the following command in the same directory as the singlestore_ mariadb file generated in the previous step. ./singlestore_mariadb./singlestore_mariadb ID: 1, Code: xvf1, Quantity: 40 ID: 2, Code: gwl2, Quantity: 15 ID: 3, Code: cdq3, Quantity: 25
Example: Connect with C
This example creates a table in SingleStore, inserts data into the table, and queries the data using C.
Note
The following commands may require sudo
access (root
privileges).
-
Install the MariaDB C connector.
This example uses a Debian-based Linux OS. apt-get install libmariadb-dev -
Add the following code to a s2example.
c file. #include <stdio.h>#include <stdlib.h>#include <mariadb/mysql.h>int main() {// Connection parameters -- REPLACE these with your actual valuesconst char *host = "<endpoint>";const char *user = "<username>";const char *pass = "<password>";const char *database = "<database>";unsigned int port = 3306;MYSQL *conn = mysql_init(NULL);// Connect to SingleStoreif (!mysql_real_connect(conn, host, user, pass, database, port, NULL, 0)) {fprintf(stderr, "Connection error: %s\n", mysql_error(conn));mysql_close(conn);return 1;}// Create a tableconst char *create_table_sql = "CREATE TABLE IF NOT EXISTS Inventory (ID INT PRIMARY KEY,Code VARCHAR(4),Quantity INT)";if (mysql_query(conn, create_table_sql)) {fprintf(stderr, "Create table error: %s\n", mysql_error(conn));mysql_close(conn);return 1;}// Insert dataconst char *insert1 = "INSERT INTO Inventory (ID, Code, Quantity) VALUES (3, 'cdq3', 25)";const char *insert2 = "INSERT INTO Inventory (ID, Code, Quantity) VALUES (1, 'xvf1', 40)";const char *insert3 = "INSERT INTO Inventory (ID, Code, Quantity) VALUES (2, 'gwl2', 15)";if (mysql_query(conn, insert1) || mysql_query(conn, insert2) || mysql_query(conn, insert3)) {fprintf(stderr, "Insert error: %s\n", mysql_error(conn));mysql_close(conn);return 1;}// Perform SELECT queryif (mysql_query(conn, "SELECT * FROM Inventory")) {fprintf(stderr, "SELECT error: %s\n", mysql_error(conn));mysql_close(conn);return 1;}MYSQL_RES *result = mysql_store_result(conn);if (result == NULL) {fprintf(stderr, "Result error: %s\n", mysql_error(conn));mysql_close(conn);return 1;}// Print resultsMYSQL_ROW row;printf("ID\tCode\tQuantity\n");while ((row = mysql_fetch_row(result))) {printf("%s\t%s\t%s\n", row[0], row[1], row[2]);}// Clean upmysql_free_result(result);mysql_close(conn);return 0;} -
Compile the code.
Run this command in the same directory as s2example. c (or append the path to the file). gcc s2example.c -o singlestore_mariadb -lmariadb -
Run the code.
Run the following command in the same directory as the singlestore_ mariadb file generated in the previous step. ./singlestore_mariadbID Code Quantity 3 cdq3 25 1 xvf1 40 2 gwl2 15
Last modified: August 11, 2025