gpt4 book ai didi

mysql - 您能否使用 pymysql 从 mysql UPDATE 查询中获取受影响的行和匹配的行?

转载 作者:行者123 更新时间:2023-11-30 21:54:01 25 4
gpt4 key购买 nike

在我的应用程序中,当我向 mysql 提交更新查询时,有三种可能的结果。

(1) 找到并更新一个匹配行

(2) 找到一个匹配的行但是更新没有效果因为它没有改变任何东西

(3) 没有找到匹配的行。

Pymysql 的 cursor.execute() 函数返回受影响的行数:在情况 (1) 中为 1,在情况 (2) 和 (3) 中为 0。

我能否区分情况 (2) 和 (3) 而无需首先进行额外查询以查看该行是否存在以及更新是否会影响它?换句话说,除了受影响的行数之外,我能否获得匹配的行数?

最佳答案

参见 github PyMySQL issues 347 comment by methane

There are two ways to know matched rows count.

  1. passing client flag

from pymysql.constants import CLIENT

conn = pymysql.connect(..., client_flag=CLIENT.FOUND_ROWS)

In this case, rowcount is rows matched with WHERE clause. (But you can't know rows changed)

  1. info

After executed query, you can show text message by print(cursor._result.message).

It's same to mysql command line client shows. https://dev.mysql.com/doc/refman/5.7/en/mysql-info.html

关于mysql - 您能否使用 pymysql 从 mysql UPDATE 查询中获取受影响的行和匹配的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46009010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com