I am building a python flask-Mysql app. I am building it using AWS cloud9. But When I run the code I am geting MYSQL_HOST key error. I am attaching code below. Is it because of the installation fault or code error.?`
JavaScript
x
28
28
1
from flask import Flask, request, render_template
2
from flask_mysqldb import MySQL
3
4
5
application = Flask(__name__)
6
7
application.config['MYSQL_HOST'] = 'localhost'
8
application.config['MYSQL_USER'] = 'nfhfjfn'
9
application.config['MYSQL_PASSWORD'] = 'fsfc'
10
application.config['MYSQL_DB'] = 'fsvf'
11
application.config['MYSQL_CURSORCLASS'] = 'DictCursor'
12
mysql = MySQL(application)
13
# mysql.init_app(application)
14
15
application = Flask(__name__)
16
17
@application.route("/")
18
def hello():
19
cursor = mysql.connect().cursor()
20
cursor.execute("SELECT * from LANGUAGES;")
21
mysql.connection.commit()
22
languages = cursor.fetchall()
23
languages = [list(l) for l in languages]
24
return render_template('index.html', languages=languages)
25
26
27
if __name__ == "__main__":
28
application.run(host='0.0.0.0',port=8080, debug=True)
`
Advertisement
Answer
You are calling application = Flask(__name__)
twice. So second time you are overwriting the first application
. It should be:
JavaScript
1
29
29
1
from flask import Flask, request, render_template
2
from flask_mysqldb import MySQL
3
4
5
application = Flask(__name__)
6
7
application.config['MYSQL_HOST'] = 'localhost'
8
application.config['MYSQL_USER'] = 'nfhfjfn'
9
application.config['MYSQL_PASSWORD'] = 'fsfc'
10
application.config['MYSQL_DB'] = 'fsvf'
11
application.config['MYSQL_CURSORCLASS'] = 'DictCursor'
12
mysql = MySQL(application)
13
# mysql.init_app(application)
14
15
#application = Flask(__name__) <--- remove that
16
17
@application.route("/")
18
def hello():
19
cursor = mysql.connect().cursor()
20
cursor.execute("SELECT * from LANGUAGES;")
21
mysql.connection.commit()
22
languages = cursor.fetchall()
23
languages = [list(l) for l in languages]
24
return render_template('index.html', languages=languages)
25
26
27
if __name__ == "__main__":
28
application.run(host='0.0.0.0',port=8080, debug=True)
29