MySQL Connector/Python 版本資訊
語法
boolean = cursor.with_rows
此唯讀屬性會傳回 True
或 False
,以指出最近執行的操作是否可能產生資料列。
當需要判斷陳述式是否產生結果集,且您需要擷取資料列時,with_rows
屬性會很有用。下列範例會擷取 SELECT
陳述式傳回的資料列,但只報告 UPDATE
陳述式受影響的資料列值
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))