示例代码:
pqxx::connection c("user=postgres");
pqxx::work txn(c);
pqxx::result r = txn.exec("SELECT d.datname as \"Name\","
"pg_catalog.pg_get_userbyid(d.datdba) as \"Owner\","
"pg_catalog.pg_encoding_to_char(d.encoding) as \"Encoding\","
"d.datcollate as \"Collate\","
"d.datctype as \"Ctype\","
"pg_catalog.array_to_string(d.datacl, E'\\n') AS \"Access privileges\""
" FROM pg_catalog.pg_database d"
" ORDER BY 1");
try {
for(int rownum=0; rownum<r.size(); ++rownum ) {
const pqxx::result::tuple row = r[rownum];
std::cout << "Column 0 Name: \'" << row[0].name() << "\': " << row[0].c_str() << std::endl;
const std::string s = "Name";
std::cout << (row[0].name() == s) << std::endl;
std::cout << "dbname: \'" << row[s].c_str() << "\'" << std::endl; // Exception here
}
} catch(const pqxx::argument_error &e) {
std::cout << "Argument Error: " << e.what() << std::endl;
}
我的输出如下
Column 0 Name: 'Name': mydb
1
Argument Error: Unknown column name: 'Name'
第一列名称为“Name”,并根据字符串“Name”测试行名称会生成一个true语句。但是当我通过该字符串访问它时,我得到一个例外。
答案 0 :(得分:4)
好的,完全是asinine。如果你想要像大写这样的东西,你必须引用这个名字。
如此改变
const std::string s = "Name";
到
const std::string s = "\"Name\"";
解决了这个问题。显然,C接口会降低您所称的内容,但不会低估其测试的字段。 ARG!我不得不通过libpqxx代码来解决这个问题。