1

示例代码:

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”测试行名称会产生一个真实的语句。但是当我通过那个字符串访问它时,我得到了一个异常。

4

1 回答 1

4

好的,完全愚蠢。如果您希望大写之类的东西起作用,则必须引用该名称。

如此变化

const std::string s = "Name";

const std::string s = "\"Name\"";

解决问题。显然,C 接口将您所称的小写,但不会将其测试所针对的字段小写。啊!我必须通过 libpqxx 代码才能弄清楚。

于 2014-09-27T06:23:18.693 回答