title: Passing mySQL cursor as return type in a python function
body: <p>I am connecting to mysql db from python using standard mysql.connector driver.</p>
  
  <p>I defined below function to create a cursor object in order to execute any db queries/DMLs</p>
  
  <pre><code>import mysql.connector as msc
      def mem_conn():
              cnx = msc.connect(user='test',password='test',host='mysql.testdb.com', database='test')
              curA = cnx.cursor(buffered=True)
              return curA
  </code></pre>
  
  <p>I make calls from a different function to get the cursor handle(curA)  that I can use to run my DML/Queries inside that function. However, my code ends up in an error when I use this solution and gives out </p>
  
  <blockquote>
    <p>File "/usr/lib/python2.6/site-packages/mysql/connector/cursor.py",
    line 482, in execute
        if not self._connection: ReferenceError: weakly-referenced object no longer exists [m</p>
  </blockquote>
  
  <p>However, if I use the same code to connect to db and use it in the same function, it works fine. </p>
  
  <p>What am I getting wrong here and what's generally accepted way to do things if I want to conenct to my db at multiple places inside my program. Transaction volume is low but I need to connect to db to read/write at lot of places inside my code.</p>
  
  <p>Thanks in advance!!</p>