连接器和 API 手册 / 第 6 章 MySQL 连接器/Python 开发人员指南 / 6.9 连接器/Python API 参考 / 6.9.5 游标.MySQL游标类 /
6.9.5.16 MySQLCursor.with_rows 属性
句法:
Press CTRL+C to copyboolean = cursor.with_rows
此只读属性返回True
or
False
以指示最近执行的操作是否已生成行。
当需要确定语句是否生成结果集并且您需要获取行时,该with_rows
属性很有用。以下示例检索SELECT
语句返回的行,但仅报告语句的 affected-rows 值
UPDATE
:
Press CTRL+C to copyimport mysql.connector cnx = mysql.connector.connect(user='scott', database='test') cursor = cnx.cursor() operation = 'SELECT 1; UPDATE t1 SET c1 = 2; SELECT 2' for result in cursor.execute(operation, multi=True): if result.with_rows: result.fetchall() else: print("Number of affected rows: {}".format(result.rowcount))