# Connect with C/C++

You can connect to your SingleStore database from C/C++ based applications using the MariaDB Connector (C/C++) (LGPLv2.1). [Download the MariaDB connector](https://mariadb.com/downloads/connectors/) version compatible with your operating system.

> **📝 Note**: As of MariaDB Connector/C++ version 1.1.6, connections configured with `rewriteBatchedStatements=true` may not work as expected. To perform multi-row inserts, ensure that your application generates a single SQL statement containing multiple rows.

Refer to MariaDB [Connector/C](https://mariadb.com/docs/connectors/mariadb-connector-c) and [Connector/C++](https://mariadb.com/docs/connectors/mariadb-connector-cpp) 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).

1. Install the required dependencies. This example uses a Debian-based Linux OS.
   ```shell
   apt-get install cmake libmariadb-dev libssl-dev
   ```

2. Download and install the MariaDB C++ connector.

   1. Clone the connector repository.
      ```shell
      git clone https://github.com/mariadb-corporation/mariadb-connector-cpp.git
      cd mariadb-connector-cpp
      ```

   2. Build and install the connector.
      ```shell
      cmake .
      make 
      make install
      ```
      The `mariadbapp` library is installed in the `/usr/local/lib` directory.

   3. Add a symbolic link for `libmariadbcpp.so`.
      ```shell
      ln -s /usr/local/lib/mariadb/libmariadbcpp.so /usr/local/lib
      ldconfig
      ```

3. Add the following code to a **s2example.cpp** file. Update the connection configuration of your SingleStore deployment in the code.
   ```C++
   #include <mariadb/conncpp.hpp>
   #include <iostream>

   int main() {
       try {
           // Connection details
           const std::string url = "tcp://<endpoint>:3306/<database>";
           const std::string user = "<username>";
           const std::string password = "<password>";

           // Connect
           sql::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 operation
   	  stmt->execute("CREATE TABLE IF NOT EXISTS Stock (ID INT PRIMARY KEY,Code VARCHAR(10),Quantity INT)");

           // Insert operation
           stmt->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 operation
           std::unique_ptr<sql::ResultSet> res(stmt->executeQuery("SELECT * FROM Stock ORDER BY ID"));

           // Display results
           while (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;
   }
   ```

4. 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).
   ```shell
   g++ s2example.cpp -o singlestore_mariadb \
       -I/usr/local/include/mariadb \
       -L/usr/local/lib/mariadb \
       -lmariadbcpp
   ```

5. Run the code. Run the following command in the same directory as the **singlestore\_mariadb** file generated in the previous step.
   ```shell
   ./singlestore_mariadb

   ```
   ```output

   ./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).

1. Install the MariaDB C connector. This example uses a Debian-based Linux OS.
   ```shell
   apt-get install libmariadb-dev
   ```

2. Add the following code to a **s2example.c** file.
   ```C
   #include <stdio.h>
   #include <stdlib.h>
   #include <mariadb/mysql.h>

   int main() {
       // Connection parameters -- REPLACE these with your actual values
       const 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 SingleStore
       if (!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 table
       const 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 data
       const 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 query
       if (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 results
       MYSQL_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 up
       mysql_free_result(result);
       mysql_close(conn);
       return 0;
   }
   ```

3. Compile the code. Run this command in the same directory as **s2example.c** (or append the path to the file).
   ```shell
   gcc s2example.c -o singlestore_mariadb -lmariadb
   ```

4. Run the code. Run the following command in the same directory as the **singlestore\_mariadb** file generated in the previous step.
   ```shell
   ./singlestore_mariadb

   ```
   ```output

   ID	Code	Quantity
   3	cdq3	25
   1	xvf1	40
   2	gwl2	15
   ```

***

Modified at: September 26, 2025

Source: [/db/v9.1/developer-resources/connect-with-application-development-tools/connect-with-c-c/](https://docs.singlestore.com/db/v9.1/developer-resources/connect-with-application-development-tools/connect-with-c-c/)

(An index of the documentation is available at /llms.txt)
