Why won't Python return my mysql-connector cursor from a function? -


python (2.7.3) violating mysql-connector cursor in strange way when return function. first example works fine...

cnx = connect() sql = "select * mytable" cursor = cnx.cursor() cursor.execute(sql) row = cursor.fetchone() 

however, if return cursor , attempt fetchone() (or fetchall()) outside, throws exception...

def run_query():     cnx = connect()     sql = "select * mytable"     cursor = cnx.cursor()     cursor.execute(sql)     return cursor  mycursor = run_query() row = mycursor.fetchone() 

it throws...

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' 

this in spite of fact "print type(mycursor)" print "mysql.connector.cursor.mysqlcursor"

what type of unholy molestation python performing on objects returned functions? (keep in mind cursors passed within module... so, it's not object passed out of "import mysql.connector" scope... )

i not have mysql available, preet sangha mentioned, when connect database inside function , return cursor, cnx variable goes out of scope when function exits, database connection closes , cursor references closed database connection.

this not case in top code example, may explain why works , why bottom example not.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -