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++?
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++?
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);
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.
© 2022 - 2024 — McMap. All rights reserved.
field::as_array
which returns anarray_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