how to make multiple requests in pqxx?

20 Views Asked by At

I'm trying to make some operations to the database, but an error occurs terminate called after throwing an instance of 'pqxx::usage_error' what(): Started nontransaction while nontransaction still active. What could be the problem?

  pqxx::nontransaction N1(*connection);
  std::string messageid_req = "select max(messageid) + 1 from server.message";
  pqxx::result messageid_res = N1.exec((char*)messageid_req.c_str());
  int messageid = messageid_res.begin()[0].as<int>();

  pqxx::nontransaction N2(*connection);
  std::string index_in_chat_req = "select max(index_in_char) + 1 from server.message";
  pqxx::result index_in_chat_res = N2.exec((char*)index_in_chat_req.c_str());
  int index_in_chat = index_in_chat_res.begin()[0].as<int>();

  std::string insert = "insert into server.message (messageid, chatid, userid, index_in_char, text)\n";
  insert += "values (" + std::to_string(messageid) + ", " + std::to_string(chatid) + ", " + std::to_string(userid) + ", " + std::to_string(index_in_chat) + text;
  pqxx::work W(*connection);
  W.exec(insert.c_str());
0

There are 0 best solutions below