当前位置:   article > 正文

mysql获取删除的条数_Python:如何使用MySQLdb获取删除的行数

mysql 变量获取删除结果

在Linux中使用Python2.7.5、MySQLdb 1.2.5版:

关键是API返回。rowcount只用于SELECT、INSERT和UPDATE。来自https://www.python.org/dev/peps/pep-0249/:.rowcount: This read-only attribute specifies the number of rows that the last .execute*() produced (for DQL statements like SELECT ) or affected (for DML statements like UPDATE or INSERT )

注意,这里有NO提到这个属性被更新为DELETE。关闭旧光标并在应用程序中创建一个新光标,在DELETE语句之后,我看到了-1 for.rowcount的结果,正如PEP249中所述。(未来注意:返回可以改为无,而不是-1。)

所以,计划b:with ... as mycursor:

mycursor.execute("DELETE FROM mytable WHERE id = %s", params=(myindex,))

mycursor.execute("SELECT ROW_COUNT() as rowcount")

rows = mycursor.fetchall()

rowcount = rows[0].get('rowcount', -1)

现在,变量rowcount具有通过执行DELETE SQL命令删除的行数。If multi is set to True, execute() is able to execute multiple statements specified in the operation string. It returns an iterator that enables processing the result of each statement. However, using parameters does not work well in this case, and it is usually a good idea to execute each statement on its own.

使用这种代码技术,我能够验证DELETE语句是否实际删除了指定的记录。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/397686
推荐阅读
相关标签
  

闽ICP备14008679号