An open API service indexing awesome lists of open source software.

https://github.com/maurodelazeri/memsql-cpp-connector

MemSQL connector with pool of connections for c++
https://github.com/maurodelazeri/memsql-cpp-connector

connector mariadb memsql pool sql

Last synced: 19 days ago
JSON representation

MemSQL connector with pool of connections for c++

Awesome Lists containing this project

README

          

# memsql-cpp-connector
Simple MemSQL connector with pool of connections for c++

### Dependencies

http://download.memsql.com/clients/mariadb-connector-c-3.0.9-linux-x86_64.tar.gz

```c++
#include
#include
#include "connection_pool.h"
#include

using namespace std;

int main(int argc, char **argv) {

std::ostringstream sqlQuery;
std::shared_ptr m_pool;
m_pool = std::make_shared("x.x.x.x", "root", "pwd", "database", 3306, 1, false);

sqlQuery << boost::format("INSERT INTO xxxx (x,x,x, x) VALUES ('%1%', '%2%', '%3%', '%4%')")
% "a"
% "b"
% "c";
shared_ptr con = m_pool->get_connection();
if (con) {
try {
con->execute(sqlQuery.str(), false, true);
m_pool->release_connection(*con);
} catch (...) {
m_pool->release_connection(*con);
}
}
return 0;
}
```

It's simple, ready to go...

You can for example check the affected rows using

```c++
std::shared_ptr affectRowsPtr = std::make_shared();
con->execute(sqlQuery.str(), false, true, affectRowsPtr.get());
if (*affectRowsPtr == 0) {
cout << "No changes" << endl;
}
```

or you can simple select data:

```c++
#include
#include
#include "connection_pool.h"
#include

using namespace std;

int main(int argc, char **argv) {

std::ostringstream sqlQuery;
auto product_id = 2;
sqlQuery << boost::format(
"select * venue from products where product_id=%1%")
% product_id;

shared_ptr con = m_pool->get_connection();
if (con) {
auto data = con->open(sqlQuery.str());
if (!data->is_valid()) {
return false;
}

auto row = data->next();
unsigned int row_idx = 0;
while (row) {
row_idx++;

cout << row->get_value(0) << endl;

row = data->next();
}

m_pool->release_connection(*con);
}

return 0;
}
```