4

I am using c++11 and pqxx to access postgresql database and I need id of inserted row and flag if it was successful or not. How to get after executing INSERT into database fetch id of inserted row ? I have tried to find example on net but without success.

work txn(*conn);
txn.prepared("insert ")(person_name).exec();
txn.commit();
PaolaJ.
  • 10,872
  • 22
  • 73
  • 111

1 Answers1

2
work txn(*conn);
pqxx::result r = txn.prepared("insert into t (a,b,c) values (1,2,$1) returning id")(person_name).exec();
txn.commit();
int id = r[0][0].as<int>();
Sheridan
  • 615
  • 1
  • 8
  • 21