【问题标题】:How to get after executing INSERT into database fetch id of inserted row?执行 INSERT 到数据库获取插入行的 id 后如何获取?
【发布时间】:2014-04-25 14:17:57
【问题描述】:

我正在使用 c++11 和 pqxx 访问 postgresql 数据库,我需要插入行的 id 和标志是否成功。 执行 INSERT 到数据库获取插入行的 id 后如何获取? 我试图在网上找到例子,但没有成功。

work txn(*conn);
txn.prepared("insert ")(person_name).exec();
txn.commit();

【问题讨论】:

  • insert ... returning?

标签: c++ postgresql c++11 libpqxx


【解决方案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>();

【讨论】:

    猜你喜欢
    • 2020-01-19
    • 2010-12-07
    • 2019-11-21
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 2016-08-15
    • 2015-03-01
    • 1970-01-01
    相关资源
    最近更新 更多