如何在postgresql的c++中使用pqxx在sql查询中执行IN
How to perform IN in sql query using pqxx in c++ for postgresql?
如何在sql查询中使用pqxx
在c++
中为postgresql
执行IN
?我有vector<long>
的id,我需要更新表学生中的每一行(将faculty_id
设置为一些新值)。我想避免循环,新值(faculty
_id)我得到当我插入教师与prepared
INSERT
语句。是否有可能传递可迭代结构或使用pqxx创建prepared
IN
查询?
void prepareChangeFaculty(connection_base &c){
const std::string sql =
"UPDATE students SET faculty_id=$2 WHERE id IN $1"; // here is a problem
c.prepare("change_faculty", sql);
}
$1我有一个向量id的行我需要更新
为什么在c++ 11
中没有类似的东西(如何连接std::string和int?)string a="";
for (int k=0;k<myVector.size();k++){
a += string(myVector[k]);
if (k<myVector.size()-1){
a += ",";
}
}
std::string sql = "UPDATE students SET faculty_id=$2 WHERE id IN (" + a + ")";
我理解@马萨的担忧,但是我找不到一个比@亚历山德罗斯不同的解决方案。
因此,可以使用std::copy 对这个解决方案进行一点改进std::stringstream params;
std::copy(
myVector.begin(), myVector.end(),
std::ostream_iterator<std::string>(params, ","));
std::string sql = "UPDATE students SET faculty_id=$2 WHERE id IN ('"
+ params.str() + "')";
相关文章:
- 使用 pqxx 将 std::vector 存储在 postgresql 中,并从数据库中检索它
- 如何使用c++在VS 2019上运行SQL查询
- System.InvalidCastException - SQL to C++ - safe_cast<float>
- 在C++中调用 MS SQL 存储过程
- 尝试创建表会给出 SQL 逻辑错误
- 在 MySQL/Connector C++中使用 SQL 联接
- Qt PL/SQL - 赋值运算符 - 字符串缓冲区太小
- 命名空间 pqxx 无法识别 - 将 libpqxx 添加到 Windows 上的 Visual Studio Post
- PostgresSQL - SQL Ready 语句与字符串转义,防止 SQL 注入攻击
- 在 Microsoft Access SQL 中调用自定义 DLL 函数时传递的内存地址无效
- 使用 SQLConfig数据源创建 SQL Server DSN 失败:关键字-值对无效
- 使用 Qt5 SQL 进行异步数据库访问的策略
- Qt/SQL - 从 QSqlQuery exec Stored Procedure 获取列类型和名称?
- 如何将QTime保存在SQL Server Express表中并读回?
- 为 Sql 服务器实现 odbc 包装器.将数据库数据读取为字符或要求驱动程序将数据转换为 C 类型
- 使用 python 或 c ++ 中的 sql 根据输入数据输出输出示例
- 无法使用 SQL VDI(虚拟设备接口)创建条带化备份
- sql记录集函数的状态设计模式
- Qt SQL LIKE语句返回错误
- 如何在postgresql的c++中使用pqxx在sql查询中执行IN