How to get after executing INSERT into database fe

2019-06-26 07:45发布

问题:

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

回答1:

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