How to get after executing INSERT into database fetch id of inserted row?

百般思念 提交于 2019-12-12 12:08:37

问题


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


来源:https://stackoverflow.com/questions/23295711/how-to-get-after-executing-insert-into-database-fetch-id-of-inserted-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!