文件首頁
MySQL Connector/Python 開發人員指南
相關文件 下載本手冊
PDF (US Ltr) - 0.7Mb
PDF (A4) - 0.7Mb


10.5.1 cursor.MySQLCursor 建構子

在大多數情況下,會使用 MySQLConnectioncursor() 方法來實例化 MySQLCursor 物件。

import mysql.connector

cnx = mysql.connector.connect(database='world')
cursor = cnx.cursor()

也可以將 MySQLConnection 物件傳遞給 MySQLCursor 來實例化游標。

import mysql.connector
from mysql.connector.cursor import MySQLCursor

cnx = mysql.connector.connect(database='world')
cursor = MySQLCursor(cnx)

connection 引數是選填的。如果省略,則會建立游標,但其 execute() 方法會引發例外。