title: Flask, Gunicorn, Mysqlconnctor /GET error on every refresh
body: <p>I was making a pretty basic Flask app to display the tables of a  mysql database on a vps.</p>
  
  <p>I ran the app via gunicorn, and it works fine and shows the tables, but every time I refresh the browser in the app, it breaks and returns the error below.</p>
  
  <p>How do I fix this and make browser refreshes work in my app?
  I have all of my files below</p>
  
  <p>GUNICORN ERROR</p>
  
  <pre><code>:8880 wsgi:app
  [2020-04-16 13:23:34 +0000] [3196] [INFO] Starting gunicorn 20.0.4
  [2020-04-16 13:23:34 +0000] [3196] [INFO] Listening at: http://0.0.0.0:8880 (319
  6)
  [2020-04-16 13:23:34 +0000] [3196] [INFO] Using worker: sync
  [2020-04-16 13:23:34 +0000] [3199] [INFO] Booting worker with pid: 3199
  [2020-04-16 13:23:37 +0000] [3196] [INFO] Handling signal: winch
  [2020-04-16 13:23:43,615] ERROR in app: Exception on / [GET]
  Traceback (most recent call last):
    File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 2447, in wsgi
  _app
      response = self.full_dispatch_request()
    File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1952, in full
  _dispatch_request
      rv = self.handle_user_exception(e)
    File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1821, in hand
  le_user_exception
      reraise(exc_type, exc_value, tb)
    File "/usr/local/lib/python3.6/dist-packages/flask/_compat.py", line 39, in re
  raise
      raise value
    File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1950, in full
  _dispatch_request
      rv = self.dispatch_request()
    File "/usr/local/lib/python3.6/dist-packages/flask/app.py", line 1936, in disp
  atch_request
      return self.view_functions[rule.endpoint](**req.view_args)
    File "/var/www/pi.sminfo.me/app.py", line 7, in display_tables
      cursor.execute("SELECT * FROM pitemp;")
    File "/home/ubuntu/.local/lib/python3.6/site-packages/mysql/connector/cursor_c
  ext.py", line 233, in execute
      raise errors.ProgrammingError("Cursor is not connected")
  mysql.connector.errors.ProgrammingError: Cursor is not connected
  [2020-04-16 13:23:47 +0000] [3196] [INFO] Handling signal: winch
  [2020-04-16 13:24:46 +0000] [3196] [INFO] Handling signal: winch
  ^C[2020-04-16 13:28:30 +0000] [3196] [INFO] Handling signal: int
  [2020-04-16 13:28:30 +0000] [3199] [INFO] Worker exiting (pid: 3199)
  [2020-04-16 13:28:30 +0000] [3196] [INFO] Shutting down: Master
  </code></pre>
  
  <p>APP.PY</p>
  
  <pre><code>from flask import Flask, render_template
  app = Flask(__name__)
  
  @app.route('/')
  def display_tables():
      cursor.execute("SELECT * FROM pitemp;")
      data = cursor.fetchall()
      db.close()
      cursor.close()
      return render_template("index.html", data=data)
  </code></pre>
  
  <p>db2.py</p>
  
  <pre><code>
  db = mysql.connector.connect (
      host="localhost",
      user="********",
      passwd="*******",
      database="*******"
  )
  
  cursor = db.cursor()
  </code></pre>
  
  <p>wgsi.py</p>
  
  <pre><code>
  if __name__ == "__main__":
      app.run()
  </code></pre>
  
  <p>/templates/index.html</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
  &lt;title&gt;Rpi Temp Log&lt;/title&gt;
  &lt;style&gt;
  
  h1 {
  color: #C93F3F;
  font-family: Verdana,Geneva,sans-serif;
  font-size: 30px;
  text-align: left;
  }
  
  table {
  border-collapse: collapse;
  width: 50%;
  color: #C93F3F;
  font-family: monospace;
  font-size: 25px;
  text-align: left;
  }
  th {
  background-color: #3A3AEA;
  color: white;
  }
  tr:nth-child(even) {background-color: #f2f2f2}
  &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;div&gt;&lt;img src="https://www.raspberrypi.org/app/uploads/2011/10/Raspi-PGB001.png" width="200" height="200" align="left"/&gt;&lt;h1&gt;Raspberry Pi Temperature Logger &lt;/h1&gt;&lt;/div&gt;
  
  
  &lt;table border="1" cellpadding="5" cellspacing="5"&gt;
  
  {% for row in data %}
     &lt;tr&gt;
      {% for d in row %}
          &lt;td&gt;{{ d }}&lt;/td&gt;
      {% endfor %}
      &lt;/tr&gt;
  {% endfor %}
  &lt;/table&gt;
  &lt;/html&gt;
  </code></pre>