我已经进行了6个月的PostGreSQL查询,并想使用 libpqxx 进行一些工作,其中我使用了来自DB的数据通过 libpqxx (通过vcpkg
获得了V6.4.5。
我的环境是Visual Studio 2019。Properties-> C/C++ -> Language : ISO C++17 Standard (/std:c++17)
在尝试编译我发现的示例时,当包含include / pqxx / pqxx中的array.hxx时,出现以下错误:
------ Build started: Project: pqxx-test2, Configuration: Debug Win32 ------
1>pqxx-test2.cpp
1>C:\Users\olebe\OneDrive\Documents\Projects\vcpkg\packages\libpqxx_x86-windows\include\pqxx\array.hxx(66,62): error C2039: 'encoding_group': is not a member of '`global namespace''
1>Done building project "pqxx-test2.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
这是我尝试编译的代码:
#include <iostream>
#include <pqxx/pqxx>
/// Query employees from database. Return result.
pqxx::result query()
{
pqxx::connection c{ "postgresql://accounting@localhost/company" };
pqxx::work txn{ c };
pqxx::result r = txn.exec("SELECT name, salary FROM Employee");
for (auto row : r)
std::cout
// Address column by name. Use c_str() to get C-style string.
<< row["name"].c_str()
<< " makes "
// Address column by zero-based index. Use as<int>() to parse as int.
<< row[1].as<int>()
<< "."
<< std::endl;
// Not really needed, since we made no changes, but good habit to be
// explicit about when the transaction is done.
txn.commit();
// Connection object goes out of scope here. It closes automatically.
return r;
}
/// Query employees from database, print results.
int main(int, char* argv[])
{
try
{
pqxx::result r = query();
// Results can be accessed and iterated again. Even after the connection
// has been closed.
for (auto row : r)
{
std::cout << "Row: ";
// Iterate over fields in a row.
for (auto field : row) std::cout << field.c_str() << " ";
std::cout << std::endl;
}
}
catch (const pqxx::sql_error & e)
{
std::cerr << "SQL error: " << e.what() << std::endl;
std::cerr << "Query was: " << e.query() << std::endl;
return 2;
}
catch (const std::exception & e)
{
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
}
还有其他人遇到过这个吗?
答案 0 :(得分:0)
错误消息“'encoding_group':不是'`global namespace'的成员”表示编译器在全局命名空间中找不到变量encoding_group的声明。因此,我的建议是找到此encoding_group的声明,并查看该变量的确切声明位置