title: Python-Flask: ReferenceError: weakly-referenced object no longer exists
body: <p>i use <code>Flask</code> framework in <code>Python</code>.I wrote this code:</p>
  
  <pre><code>from flask import *
  import mysql.connector
  def connect_db():   
          db = mysql.connector.Connect(host='***',user='***',password='**',database='***')
          return db.cursor()
  
  def query_db(query, args=(), one=False):
      cur = g.db  
      cur.execute(query, args)
      rv = [dict((cur.description[idx][0], value)
      for idx, value in enumerate(row)) for row in cur.fetchall()]
      return (rv[0] if rv else None) if one else rv
  
      @app.before_request
      def before_request():
          g.db = connect_db()
          blogOptions = query_db('select * from tbl_options',None)    
  
      @app.route("/")
      def index():    
          return render("index.html")
  </code></pre>
  
  <p>When i run this code.It says </p>
  
  <pre><code>ReferenceError: weakly-referenced object no longer exists
  </code></pre>
  
  <p>with the traceback:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/Users/ozcan/Documents/python/venv/lib/python2.7/site-packages/flask/app.py", line 1701, in __call__
      return self.wsgi_app(environ, start_response)
    File "/Users/ozcan/Documents/python/venv/lib/python2.7/site-packages/flask/app.py", line 1689, in wsgi_app
      response = self.make_response(self.handle_exception(e))
    File "/Users/ozcan/Documents/python/venv/lib/python2.7/site-packages/flask/app.py", line 1687, in wsgi_app
      response = self.full_dispatch_request()
    File "/Users/ozcan/Documents/python/venv/lib/python2.7/site-packages/flask/app.py", line 1360, in full_dispatch_request
      rv = self.handle_user_exception(e)
    File "/Users/ozcan/Documents/python/venv/lib/python2.7/site-packages/flask/app.py", line 1356, in full_dispatch_request
      rv = self.preprocess_request()
    File "/Users/ozcan/Documents/python/venv/lib/python2.7/site-packages/flask/app.py", line 1539, in preprocess_request
      rv = func()
    File "/Users/ozcan/Documents/python/app.py", line 124, in before_request
      blogOptions = query_db('select * from tbl_options',None)
    File "/Users/ozcan/Documents/python/app.py", line 47, in query_db
      cur.execute(query, args)
    File "/Users/ozcan/Documents/python/venv/lib/python2.7/site-packages/mysql/connector/cursor.py", line 364, in execute
      if self._have_unread_result():
    File "/Users/ozcan/Documents/python/venv/lib/python2.7/site-packages/mysql/connector/cursor.py", line 193, in _have_unread_result
      return self._connection.unread_result
  ReferenceError: weakly-referenced object no longer exists
  </code></pre>
  
  <p>How can i fix this issue?</p>