如何使用libpqxx从数组中获取值?例如,我有这样的表:
CREATE TABLE testTable
(
testArray integer[]
);
如何在C ++中使用这些值获取int数组?
答案 0 :(得分:1)
在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);
答案 1 :(得分:0)
我还没有找到除此之外的任何其他方式:
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;
}
}
我暂时使用它,但我认为这不是最佳方式。