未在postgresql中执行Delete语句
我正在创建一个事务,其中我要更新一个表中的用户,并删除另一个表中属于该用户的一些数据。但只执行第一个查询,而不执行第二个查询。在第二个查询未在postgresql中执行Delete语句,postgresql,c++11,libpqxx,Postgresql,C++11,Libpqxx,我正在创建一个事务,其中我要更新一个表中的用户,并删除另一个表中属于该用户的一些数据。但只执行第一个查询,而不执行第二个查询。在第二个查询code中的delete语句中,是一个逗号分隔的std::string pqxx::connection c(connectionString); try { pqxx::work w(c); pqxx::result r; c.prepare("user", "update user set started = null, fin
code
中的delete语句中,是一个逗号分隔的std::string
pqxx::connection c(connectionString);
try {
pqxx::work w(c);
pqxx::result r;
c.prepare("user", "update user set started = null, finished = null, task = $1 where id = $2");
r = w.prepared("user")(task)(email).exec();
c.prepare("belongings", "delete from belongings where id in " \
"(select id from info where code in ($1) and id = $2)");
r = w.prepared("belongings")(code)(id).exec();
w.commit();
}
我读了一篇关于如何在commit()之前运行多个查询的文章。因此,我必须在第二个delete语句中出错,但找不到原因 将
code
参数解释为单个文本。您可以尝试使用any(数组表达式)的替代语法,
code = "{abc,def}"; // instead of code = "'abc','def'"
...
c.prepare("belongings", "delete from belongings where id in " \
"(select id from info where code = any ($1) and id = $2)");
r = w.prepared("belongings")(code)(id).exec();
属性的类型是什么。code?
还显示code
变量的赋值。这并不能回答您的问题,但您可以将所有这些都封装在一个函数中并执行该函数。这将有一个额外的好处,即如果第一条语句失败,它将不会执行第二条语句(尽管一个事务可以完成同样的事情)。@klin propertings.code是表中的varchar。代码是一个std::字符串,比如'abc','def','ghi'。我最初认为IN条款有限制,但事实似乎并非如此。这很有帮助!