当我从函数返回时,Python(2.7.3)以某种奇怪的方式违反了我的 mysql-connector 光标。这第一个例子工作正常......
cnx = connect()
sql = "SELECT * FROM MyTable"
cursor = cnx.cursor()
cursor.execute(sql)
row = cursor.fetchone()
但是,如果我返回游标并从外部尝试 fetchone()(或 fetchall()),则会引发异常......
def run_query():
cnx = connect()
sql = "SELECT * FROM MyTable"
cursor = cnx.cursor()
cursor.execute(sql)
return cursor
mycursor = run_query()
row = mycursor.fetchone()
它抛出...
File "/usr/lib/pymodules/python2.7/mysql/connector/cursor.py", line 533, in fetchone
row = self._fetch_row()
File "/usr/lib/pymodules/python2.7/mysql/connector/cursor.py", line 508, in _fetch_row
(row, eof) = self.db().protocol.get_row()
AttributeError: 'NoneType' object has no attribute 'protocol'
尽管“打印类型(mycursor)”将打印“mysql.connector.cursor.MySQLCursor”这一事实
Python 对从函数返回的对象执行什么类型的恶意骚扰?(请记住,它将对模块内传递的游标执行此操作......所以,它不像传递出“import mysql.connector”范围的对象......)