简体   繁体   English

C ++ pqxx工作事务崩溃

[英]C++ pqxx work transaction crashing

I'm working with the c++ PostgreSQL libpqxx library and I'm not sure why I can't open a transaction/nontransaction object. 我正在使用c ++ PostgreSQL libpqxx库,但不确定为什么不能打开事务/非事务对象。

My code is currently as follows 我的代码当前如下

#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();

}

It's making the connection to the database just fine and preparing my sql fine as well but it is failing to compile (I'm compiling on CentOS 5 btw) 它可以很好地连接数据库,并可以很好地准备我的sql,但编译失败(我在CentOS 5 btw上进行编译)

g++ -o ./pgsqltest ./pgsqltest02.cpp -lpqxx

gives me the following errors 给我以下错误

./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>&)

I'm not sure why this would be, maybe I'm missing something very obvious 我不确定为什么会这样,也许我错过了一些非常明显的东西

The error had to do with the compile command, it needed additional arguments that point to the libpqxx headers and binaries. 该错误与compile命令有关,它需要其他指向libpqxx标头和二进制文件的参数。 The correct compile command was 正确的编译命令是

g++ -o ./pgsqltest ./pgsqltest02.cpp -I /opt/libpqxx/include -L /opt/libpqxx/lib -lpq -lpqxx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM