如何使用libpqxx从数组中获取值?例如,我有这样的表格:
CREATE TABLE testTable
(
testArray integer[]
);
如何在C++中获得具有这些值的int数组?
在7.2版本中,我使用as_array
方法解析整数数组:
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);
我没有找到其他方法:
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;
}
}
暂时我用这个,但我认为这不是最好的方法。