postgresql libpqxx Several queries as one transaction

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-04 02:29:17

问题


Is it possible to execute one transaction contains several queries, for example insert smth in table1 and insert smth in table2? How I can implement this? I use libpqxx to interact the database and expect an answer relating to that. Thank you.


回答1:


pqxx::work is a default transaction type. Use multiple exec() method before commit() to run multiple queries in one transaction:

using namespace pqxx;
...
  connection c("dbname=test user=postgres hostaddr=127.0.0.1");
  work w(c);
  w.exec("create table test_xx (id int primary key)");
  w.exec("insert into test_xx values (1)");
  w.commit();
...   


来源:https://stackoverflow.com/questions/34461366/postgresql-libpqxx-several-queries-as-one-transaction

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