7

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

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.