How can I get values from array using libpqxx?
Asked Answered
A

2

6

How can I get values from array using libpqxx? For Example I have table like this:

CREATE TABLE testTable
(
 testArray integer[]
);

How do I get int array with these values in C++?

Alienable answered 28/10, 2015 at 15:41 Comment(0)
P
4

In the version 7.2 I am using the as_array method to parse an array of integers:

list<unsigned long> found_messages;
pqxx::result res = db->execute(query);
auto arr = res[0][0].as_array();
pair<pqxx::array_parser::juncture, string> elem;
do
{
    elem = arr.get_next();
    if (elem.first == pqxx::array_parser::juncture::string_value)
        found_messages.push_back(stoul(elem.second));
}
while (elem.first != pqxx::array_parser::juncture::done);
Polypody answered 12/10, 2020 at 19:17 Comment(0)
A
0

I haven't found any way other than that:

result wynikiJednostki(ncWykonaj.exec("SELECT * FROM testTable;"));
result::const_iterator rekord = wynikiJednostki.begin()
vector<unsigned short>* values = getArray(record[1].as<string>());

getArray:

vector<unsigned short>* getArray(string pString)
{
vector<unsigned short>* numbers = new vector<unsigned short>;

pString += ',';
size_t begin = pString.find('{') + 1;
size_t end = pString.find('}');
size_t actualPosition;
string numberS;

while(true)
{
    actualPosition = pString.find(',', begin);
    if(actualPosition > end)
    {
        numberS = pString.substr(begin, end - begin);
        numbers->push_back(atoi(numberS.c_str()));

        return numbers;
    }
    numberS = pString.substr(end, actualPosition - begin);
    numbers->push_back(atoi(numberS.c_str()));
    begin = actualPosition + 1;
}
}

Temporarily I use that, but I think it isn't the best way.

Alienable answered 1/11, 2015 at 21:32 Comment(1)
There (now? maybe wasn't in 2015) exists field::as_array which returns an array_parser object that seems to do the trick, but some of its intricacies are undocumented... if I get it working I might add a more detailed answer.Hennebery

© 2022 - 2024 — McMap. All rights reserved.