title: should I reuse the cursor in the python MySQLdb module
body: <p>I'm writing a python CGI script that will query a MySQL database.  I'm using the MySQLdb module.  Since the database will be queryed repeatedly, I wrote this function....</p>
  
  <pre><code>def getDatabaseResult(sqlQuery,connectioninfohere):
      # connect to the database
      vDatabase = MySQLdb.connect(connectioninfohere)
      # create a cursor, execute and SQL statement and get the result as a tuple
      cursor = vDatabase.cursor()
      try:
          cursor.execute(sqlQuery)
      except:
          cursor.close()
          return None
      result = cursor.fetchall()
      cursor.close()
      return result
  </code></pre>
  
  <p>My question is...  Is this the best practice?  Of should I reuse my cursor within my functions?  For example.  Which is better...</p>
  
  <pre><code>def callsANewCursorAndConnectionEachTime():
      result1 = getDatabaseResult(someQuery1)
      result2 = getDatabaseResult(someQuery2)
      result3 = getDatabaseResult(someQuery3)
      result4 = getDatabaseResult(someQuery4)
  </code></pre>
  
  <p>or do away with the getDatabaseeResult function all together and do something like..</p>
  
  <pre><code>def reusesTheSameCursor():
      vDatabase = MySQLdb.connect(connectionInfohere)
      cursor = vDatabase.cursor()
  
      cursor.execute(someQuery1)
      result1 = cursor.fetchall()
  
      cursor.execute(someQuery2)
      result2 = cursor.fetchall()
  
      cursor.execute(someQuery3)
      result3 = cursor.fetchall()
  
      cursor.execute(someQuery4)
      result4 = cursor.fetchall()
  </code></pre>