- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在努力生成删除查询,其中查询的参数实际上是一组值。
所以我需要删除参数是一对值的行,例如:
从表中删除 col1 = %s 和 col2 = %s
可以在 Python 中执行,如:
cur = conn.cursor()
cur.execute(query, (col1_value, col2_value))
现在我想运行一个查询:
delete from table where (col1, col2) in ( (col1_value1, col2_value1), (col1_value2, col2_value2) );
我可以生成查询和值并执行准确的 SQL,但我不能完全生成准备好的语句。
我试过:
delete from table where (col1, col2) in %s
和
delete from table where (col1, col2) in (%s)
但是当我尝试执行时:
cur.execute(query, list_of_col_value_tuples)
或
cur.execute(query, tuple_of_col_value_tuples)
我收到一个异常,表明 psycopg2 无法将参数转换为字符串。
有什么方法可以使用 psycopg2 来执行这样的查询吗?
最佳答案
您可以动态地将 %s
占位符添加到您的查询中:
cur = con.cursor()
query = "delete from table where (role, username) in (%s)"
options = [('admin', 'foo'), ('user', 'bar')]
placeholders = '%s,' * len(options)
query = query % placeholders[:-1] # remove last comma
print(query)
print(cur.mogrify(query, options).decode('utf-8'))
输出:
delete from table where (role, user) in (%s,%s)
delete from table where (role, user) in (('admin', 'foo'),('user', 'bar'))
或者,使用 psycopg2.sql
构建查询作为回答 there .
关于python - psycopg2 准备删除语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66768703/
我一直在试图找出为什么这会给我一个错误: PREPARE test FROM 'SELECT t.blah FROM (SELECT ? AS blah) t;'; ERROR 1054 (42S22
我想了解 Go 中的 channel 。我读过默认情况下发送和接收 block ,直到发送方和接收方都准备好。但是我们如何确定发送方和接收方的准备情况。 例如在下面的代码中 package main
大部分的pytorch入门教程,都是使用torchvision里面的数据进行训练和测试。如果我们是自己的图片数据,又该怎么做呢? 1、我的数据 我在学习的时候,使用的是fashion-mnis
在我的 Storyboard中,controller1 有 natigationController 和 segues 到 controller 2。奇怪的是 Segue 工作正常但是 overrid
我正在尝试使用Openshift在线从我的仓库中放入一些代码。 我的构建编译正常,但是部署失败: error: update acceptor rejected nodejs-mongo-persis
已结束。此问题不符合 Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是无关紧要的,
我有一个每晚运行的存储过程。 它从链接服务器中提取一些数据并将其插入到运行 sql 代理作业的服务器上的表中。在运行 INSERT 语句之前,该过程检查链接服务器上的数据库是否联机 (STATE =
我对 PDO 准备好的语句比较陌生。 我忍不住觉得必须有一种更简单、更整洁的方法来执行以下操作:我无论如何都将所有内容加载到一个数组中,然后必须将整个内容重写为一组 ':blah '=>$found[
我正在准备一个 pandas 数据框,用于在 R 的 ggplot2 中绘制带有误差条的图,这需要计算列的统计数据。误差条需要最小值(平均值 - 标准偏差)和最大值(平均值+标准差)。我使用 grou
我的 prepareForSegue 方法没有将数据传递到目标 View Controller 。 var buttonsDictionary = [Int: UIButton]() func cre
需要帮助来完成这个简单的任务。该 sp 应该提供一个结果集,并将从 MS-Access-Database 报告目的中调用。 表名可变,但以数字 (lsid) 结尾。使用串联。max_prepared_
我正在为我工作的小型企业创建销售订单表单。我有一个表单连接到数据库以获取下拉菜单,然后连接到文本框以在需要时添加新数据。如果前面的部分中的所有数据框均已填写,还会出现另外四个部分。 链接到数据库的
以下代码: class Database { (...) public function query($query){ $this->stmt = $this->dbh
我正在开展一个学校项目,涉及一个具有数据库集成的网站。目前正致力于通过它向网站添加新内容(文本、标题、图像)。我已经可以通过网站向数据库添加新用户,但由于某种原因,相同的代码和逻辑不适用于内容。 我注
我如何mysqli::stmt->bind_param在mysql中被视为NULL的东西? 我目前正在使用 $stmt->bind_param('s', 'NULL'); 最佳答案 bind_para
我在此处运行准备 stmt 时遇到语法错误。我尝试手动执行 @c 中的查询并且它有效。不知道为什么 stmt 会给出这个错误。这是我正在使用的代码。 SET @i=24; SET @Bill_mont
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我想知道准备好的查询是否与未准备的查询一样安全。下面是两个示例,一个用于 SELECT,一个用于 UPDATE。第一行是未准备的查询,第二行是准备好的查询。 选择示例: $userDetails =
我是 ios 开发的新手,在我尝试做的事情上遇到了障碍。 我的方法中有这段代码,旨在将特定的 NSObject 从这个 View 传递到下一个 View ,但是它总是让我的应用程序崩溃。 -(void
自上次调用 segue 以来,我的 UIButton 类的 statValue 属性已更新,但 segue 仍发送旧的原始值。有没有办法刷新 prepare 函数(如下),以便它发送新值? overr
我是一名优秀的程序员,十分优秀!