赞
踩
在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语句是否实际删除了指定的记录。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。