C++ Driver

The SQream DB C++ driver allows C++ programs and tools to connect to SQream DB.

This tutorial shows how to write a C++ program that uses this driver.

Installing the C++ Driver

Prerequisites

The SQream DB C++ driver was built on 64-bit Linux, and is designed to work with RHEL 7 and Ubuntu 16.04 and newer.

Getting the Library

The C++ driver is provided as a tarball containing the compiled libsqream.so file and a header sqream.h. Get the driver from the SQream Drivers page. The library can be integrated into your C++-based applications or projects.

Extract the Tarball Archive

Extract the library files from the tarball

$ tar xf libsqream-3.0.tar.gz

Examples

Assuming there is a SQream DB worker to connect to, we’ll connect to it using the application and run some statements.

Testing the Connection to SQream

Download this file by right clicking and saving to your computer connect_test.cpp.

Connect to SQream DB
 1// Trivial example
 2
 3#include <iostream>
 4
 5#include  "sqream.h"
 6
 7int main () {
 8
 9   sqream::driver sqc;
10
11   // Connection parameters: Hostname, Port, Use SSL, Username, Password,
12   // Database name, Service name
13   sqc.connect("127.0.0.1", 5000, false, "rhendricks", "Tr0ub4dor&3",
14               "raviga", "sqream");
15
16   // create table with data
17   run_direct_query(&sqc, "CREATE TABLE test_table (x int)");
18   run_direct_query(&sqc, "INSERT INTO test_table VALUES (5), (6), (7), (8)");
19
20   // query it
21   sqc.new_query("SELECT * FROM test_table");
22   sqc.execute_query();
23
24   // See the results
25   while (sqc.next_query_row()) {
26       std::cout << "Received: " << sqc.get_int(0) << std::endl;
27   }
28
29   sqc.finish_query();
30
31   // Close the connection completely
32   sqc.disconnect();
33
34}

Compiling and Running the Application

To build this code, place the library and header file in ./libsqream-3.0/ and run

$ g++ -Wall -Ilibsqream-3.0 -Llibsqream-3.0 -lsqream connect_test.cpp -o connect_test
$ ./connect_test

Modify the -I and -L arguments to match the .so library and .h file if they are in another directory.

Creating a Table and Inserting Values

Download this file by right clicking and saving to your computer insert_test.cpp.

Inserting data to a SQream DB table
 1// Insert with parameterized statement example
 2
 3#include <iostream>
 4
 5#include  "sqream.h"
 6
 7int main () {
 8
 9   sqream::driver sqc;
10
11   // Connection parameters: Hostname, Port, Use SSL, Username, Password,
12   // Database name, Service name
13   sqc.connect("127.0.0.1", 5000, false, "rhendricks", "Tr0ub4dor&3",
14               "raviga", "sqream");
15
16   run_direct_query(&sqc,
17       "CREATE TABLE animals (id INT NOT NULL, name VARCHAR(10) NOT NULL)");
18
19   // prepare the statement
20   sqc.new_query("INSERT INTO animals VALUES (?, ?)");
21   sqc.execute_query();
22
23   // Data to insert
24   int row0[] = {1,2,3};
25   std::string row1[] = {"Dog","Cat","Possum"};
26   int len = sizeof(row0)/sizeof(row0[0]);
27
28   for (int i = 0; i < len; ++i) {  
29      sqc.set_int(0, row0[i]);
30      sqc.set_varchar(1, row1[i]);
31      sqc.next_query_row();
32   }  
33
34   // This commits the insert
35   sqc.finish_query(); 
36
37   sqc.disconnect();
38
39}

Compiling and Running the Application

To build this code, use

$ g++ -Wall -Ilibsqream-3.0 -Llibsqream-3.0 -lsqream insert_test.cpp -o insert_test
$ ./insert_test