0

I've deployed a form website which can insert the user's input to the MySQL database onto heroku with ClearDB. But when I submit the form, I either get the error [2055 Lost connection to MySQL server] or [2013: Lost connection to MySQL server during query]. The funny thing is, when I insert data into the ClearDB database on my local machine, it works perfectly. Below is my server code.

from flask import Flask, render_template, url_for, request, jsonify
import mysql.connector

db = mysql.connector.connect(
    host = "HOST",
    user = "USERNAME",
    password = "PASSWORD",
    database = "DATABASE",
    buffered = True,
    use_pure=True
)
myCursor = db.cursor(buffered=True) 

app = Flask(__name__)

@app.route("/", methods=['GET', 'POST'])
def main_page():
    return render_template("main.html") 

@app.route("/pare_1", methods = ["POST", "GET"])
def page_1():
    if request.method == "POST": 
        myCursor.execute("INSERT INTO TABLE_NAME_2 (COLUMN1, COLUMN2, COLUMN3) VALUES (%s, %s, %s)", (value1, value2, value3))
        db.commit()
    return render_template("page_1.html")

@app.route("/page_2", methods = ["POST", "GET"])
def page_2():
    if request.method == "POST": 
        myCursor.execute("INSERT INTO TABLE_NAME_2 (COLUMN1, COLUMN2, COLUMN3) VALUES (%s, %s, %s)", (value1, value2, value3))
        db.commit()
    return render_template("page_2.html")

if __name__ == "__main__":
    app.debug = True
    app.run()

I've been looking for some possible solutions. Some suggest that switch mysql.connetor to flaskext.mysql be the solution. I'm a bit confused. Is it really mysql.connector's fault? Or there's something else that gets wrong?

Weber Chang
  • 45
  • 1
  • 7

1 Answers1

1

I've figured this out. Turns out, I didn't close my cursor and connection to the database after every query.

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Weber Chang
  • 45
  • 1
  • 7