postgresql libpqxx几个查询作为一个事务 [英] postgresql libpqxx Several queries as one transaction

查看:267
本文介绍了postgresql libpqxx几个查询作为一个事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以执行一个包含多个查询的事务,例如在表1中插入smth和在表2中插入smth?我该如何实施?我使用 libpqxx 与数据库进行交互,并期待与此相关的答案。谢谢。

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.

推荐答案

pqxx :: work 是默认交易类型。
commit()之前使用多个 exec()方法在一个事务中运行多个查询:

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

这篇关于postgresql libpqxx几个查询作为一个事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆