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