3

如何使用 libpqxx 从数组中获取值?例如我有这样的表:

CREATE TABLE testTable
(
 testArray integer[]
);

如何在 C++ 中获得具有这些值的 int 数组?

4

2 回答 2

2

在 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);
于 2020-10-12T19:17:25.280 回答
0

除此之外,我还没有找到任何方法:

result wynikiJednostki(ncWykonaj.exec("SELECT * FROM testTable;"));
result::const_iterator rekord = wynikiJednostki.begin()
vector<unsigned short>* values = getArray(record[1].as<string>());

获取数组:

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;
}
}

我暂时使用它,但我认为这不是最好的方法。

于 2015-11-01T21:32:04.737 回答