#include <iostream>
#include <pqxx/pqxx>
using namespace std;
using namespace pqxx;
int main() {
// define variables
string params;
connection* pgsql;
string sql;
// db connection parameters
params = "dbname=dummy user=postgres password=postgres hostaddr=10.10.0.2 port=5431";
try {
// make connection
pgsql = new connection(params);
} catch(const exception &log) {
// connection failed
cerr << log.what() << endl;
}
// prepare sql
sql = " select * from categories where cat_idno = $1 ";
pgsql->prepare("categories", sql)("integer");
// execute transaction
// this bit doesn't work
work tr(pgsql);
result row = tr.prepared("categories")(1).exec();
tr.commit();
// disconnect
pgsql->disconnect();
}
它可以很好地连接到数据库,也可以很好地准备sql,但无法编译(我在CentOS 5 btw上编译)
g++ -o ./pgsqltest ./pgsqltest02.cpp -lpqxx
给我以下错误
./pgsqltest02.cpp: In function âint main()â:
./pgsqltest02.cpp:24: error: no matching function for call to âpqxx::transaction<read_committed, read_write>::transaction(pqxx::connection*&)â
/usr/include/pqxx/transaction.hxx:102: note: candidates are: pqxx::transaction<ISOLATIONLEVEL, READWRITE>::transaction(pqxx::connection_base&) [with pqxx::isolation_level ISOLATIONLEVEL = read_committed, pqxx::readwrite_policy READWRITE = read_write]
/usr/include/pqxx/transaction.hxx:97: note: pqxx::transaction<ISOLATIONLEVEL, READWRITE>::transaction(pqxx::connection_base&, const std::string&) [with pqxx::isolation_level ISOLATIONLEVEL = read_committed, pqxx::readwrite_policy READWRITE = read_write]
/usr/include/pqxx/transaction.hxx:87: note: pqxx::transaction<read_committed, read_write>::transaction(const pqxx::transaction<read_committed, read_write>&)
我不知道为什么会这样,也许我错过了一些非常明显的东西