10.5.1 cursor.MySQLCursor构造函数

在大多数情况下,该MySQLConnection cursor() 方法用于实例化一个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)

连接参数是可选的。如果省略,则会创建游标,但其 execute() 方法会引发异常。