MariaDB C++ Connector Setup for MacOS

For some reason, when I was setting this up, I was having a hard time getting it to work. Save myself the hassle next time by having step by step notes on what I did to get it working. Also has the added benefit of helping someone else if they ever stumble on this.

Install MariaDB using brew

brew install mariadb
This is to get the mariadb C connector which is packaged with the full release/build of mariadb server.
After installing it, you don't have to start up the server if you don't need to. I'm personally hosting an instance through docker.

Setup your project

Going to reference this as helloMaria.

Let's assume you to create your project folder

mkdir helloMaria && cd helloMaria

# Don't really need to git init this in here
#   but it's just to illustrate the usage of git submodule
git init

# CMakeLists for building.
touch CMakeLists.txt

I'm using CLion, so the project directory is already created with a CMakeLists.txt already.

Get MariaDB C++ bindings

Note: I think I had problems last time because I didn't install the full MariaDB. The C++ bindings attach to the mariaDB C connector.

So within your project's folder, do a git clone to the project and follow the instructions according viaduck's project.

# assuming your are in the helloMaria dir
git clone https://github.com/viaduck/mariadbpp.git
cd mariadbpp
git submodule update --init
# now head back to the root project folder
cd ..

This is for the local version. The project Readme outlines a way to build and install for system-wide usage.

Set up cmake

Set it up by modifying CMakeLists. I'm using CLion so it creates the file for me already.

# assuming your are in the helloMaria dir
nano CMakeLists.txt

The CMake I used for MacOS is:

cmake_minimum_required(VERSION 3.17)
project(helloMaria)

set(CMAKE_CXX_STANDARD 20)
add_subdirectory(./mariadbpp)

set(SOURCES main.cpp)
add_executable(${PROJECT_NAME} ${SOURCES})

target_link_libraries(${PROJECT_NAME} mariadbclientpp)

Build and Run helloMaria project to test.


#include <iostream>
#include <string>
#include <mariadb++/connection.hpp>

int main() {

    // set up the account
    mariadb::account_ref acc = mariadb::account::create(
            "127.0.0.1", "username_string", "password_string",
            "hello", 3306, "");
        //  Note: the arguments in order (host_name, user_name, password, schema(database), port, unix_socket)

    // create connection
    mariadb::connection_ref con = mariadb::connection::create(acc);

    // create statement object
    mariadb::statement_ref stmt = con->create_statement("SELECT * FROM versionTable;");
    mariadb::result_set_ref result = stmt->query();

    // reading from a result set
    while (result->next()) {
        std::string a = result->get_string(0);
        std::string b = result->get_string("scriptVersion");

        std::cout << a << " " << b << std::endl;
    }

    return 0;
}


And that's how I got it to work.


You'll only receive email when they publish something new.

More from Wron
All posts