Getting Started

This section provides a short demo to help you get started with the DolphinDB C++ API.

By the end of this section, you will be able to connect to a standalone DolphinDB server and interact with it using the DolphinDB C++ API to perform database operations.

Prerequisites

Environment Setup

Make sure you have an Environment set up with DolphinDB C++ API shared libraries and executable files compiled.

Connecting to Server

The most important class provided by DolphinDB C++ API is DBConnection.It facilitates communication between the client and the DolphinDB server. Through DBConnection, you can execute scripts and functions on the DolphinDB server and transfer data in both directions.

Establish a connection to the DolphinDB server and try to perform a simple operation with the following script:
#include "DolphinDB.h"
#include <iostream>
using namespace dolphindb;

int main(int argc, char* argv[]) {
    DBConnection conn;
    bool ret = conn.connect("127.0.0.1", 8848);
    if (!ret) {
        std::cout << "Failed to connect to the server" << std::endl;
        return 0;
    }
    ConstantSP result= conn.run("1+1");
    std::cout << result->getString() << std::endl;
    return 0;
}

For the script above,

  • with line 7, conn.connect() connects to the DolphinDB server;
  • with line 8, verifies whether the connection has been successfully established or not;
  • with line 12, conn.run() executes the operation “1+1”.

Interacting with Server

This section briefly introduces how to upload and download data with DBConnection.

Establish a connection to the DolphinDB server and try to upload and download a string with the following script:

#include "DolphinDB.h"
#include <iostream>
using namespace dolphindb;

int main(int argc, char* argv[]) {
    DBConnection conn;
    bool ret = conn.connect("127.0.0.1", 8848, "admin", "123456");
    if (!ret) {
        std::cout << "Failed to connect to the server" << std::endl;
        return 0;
    }
    ConstantSP s = Util::createString("123");
    conn.upload("s", s);
    ConstantSP r = conn.run("s");
    std::cout << r->getString() << std::endl;
    return 0;
}

For the script above,

  • with line 13, conn.upload() uploads a string “s“ created in line 12 to s stored in DolphinDB;
  • with line 14, conn.run() reads string “s“ back and it is printed in line 15.

Database Operations

This section briefly introduces how to operate on a DolphinDB database with DBConnection.

Establish a connection to the DolphinDB server and try to operate on a DolphinDB database with the following script:

int main(int argc, const char **argv)
{
    DBConnection conn;
    bool ret = conn.connect("127.0.0.1", 8848, "admin", "123456");
    if (!ret) {
        std::cout << "Failed to connect to the server" << std::endl;
        return 0;
    }
    conn.run(R"(
        n=1000000
        ID=rand(10, n)
        x=rand(1.0, n)
        t=table(ID, x)
        db=database(directory="dfs://hashdb", partitionType=HASH, partitionScheme=[INT, 2])
        pt = db.createPartitionedTable(t, `pt, `ID)
        pt.append!(t);
    )");
    ConstantSP re = conn.run("select count(x) from pt;");
    std::cout << re->getString() << std::endl;
    return 0;
}

The script above

  • with line 9-17, creates a DolphinDB database “dfs://hashdb“ and table “pt“; appends data to table “pt“;
  • with line 18, queries data with the SQL statement select count(x) from pt;.