Documentation Home

6.9.5.16 MySQLCursor.with_rows 属性

句法:

Press CTRL+C to copy
boolean = cursor.with_rows

此只读属性返回Trueor False以指示最近执行的操作是否已生成行。

当需要确定语句是否生成结果集并且您需要获取行时,该with_rows属性很有用。以下示例检索SELECT 语句返回的行,但仅报告语句的 affected-rows 值 UPDATE

Press CTRL+C to copy
import 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))