示例代码:
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语句。但当我用那个字符串访问它时,我会得到一个异常。
好的,完全愚蠢。如果你想让大写字母之类的东西起作用,你必须引用名字。
所以改变
const std::string s = "Name";
至
const std::string s = ""Name"";
修复了问题。显然,C接口降低了您所称的大小写,但没有降低测试所针对的字段的大小写。ARG!我必须遍历libpqxx代码才能弄清楚。