4

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++?

ktoś tam
  • 739
  • 1
  • 6
  • 11

2 Answers2

3

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);
karastojko
  • 1,156
  • 9
  • 14
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.

ktoś tam
  • 739
  • 1
  • 6
  • 11
  • 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. – celticminstrel May 31 '19 at 22:28