Loading…

The missing pqxx tablewriter example

Did you ever Google for “pqxx tablewriter example”? Well, if you did then you most certainly noticed that there are roughly 500 results but not a single full example. Or maybe I am either too stupid to operate Google or too lazy to click through all result pages.

Intro TL;DR: here is the missing pqxx tablewriter example:

pqxx::connection conn("postgresql://postgres@localhost/db");
pqxx::work work(conn);

const std::string targetTable = "sometable";
pqxx::tablewriter writer(work, targetTable);

std::vector<std::array<std::string, 4>> rows = getMyData();
for (const auto& row : rows) writer << row;

writer.complete();
work.commit();

Basically std::vector<std::array<std::string, 4>> rows and writer << row is what I was stumbling over all time. I am just not sure now whether one could also pass native data into the writer. But as it seems, it always wants a to_string conversion method. Maybe anybody knows better?

Leave a Reply