title: How do I use a MySQLdb connect object on imported function
body: <p>Noobish question to be sure. I have tried to move some common code into a separate module and use the code as an imported function. The code makes a MySQL query through MySQLdb. When the function is part of the main script, it runs just fine. When I import the function from a separate module, the function fails because the cursor object is no longer defined. Is there a way to import functions without defining a separate cursor object for just the imported function?</p>
  
  <p>Here is an coded example. This works:</p>
  
  <pre><code>import MySQLdb
  #from mod2 import lookup_value
  
  def get_db_connection(database_name):
      db = MySQLdb.connect('localhost', 'user', 'pswrd', database_name)
      cur = db.cursor()
      return db, cur
  
  def lookup_value(user_name):
      query = "SELECT COUNT(*) FROM x_user_%s" % (user_name)
      cur.execute("%s" % (query))
      return cur.fetchone()
  
  db_name = 'mg_test' # database name
  user_name = 'test' # name of a specific table in the database
  
  db, cur = get_db_connection(db_name)
  value = lookup_value(user_name)
  </code></pre>
  
  <p>When I move the code for lookup_value to a second file, and import it ('from mod2 import lookup_value'), the code fails because the cursor object is undefined. The imported version of lookup_value only works if I create a cursor object for its use. This seems very inefficient. What is the best way to handle this problem?</p>