I'm trying to run a flask app together with a MySQL server. But I can't seem to get pass the connect-to-db stage. My app.py has the following code:
from flask import Flask, render_template
from flaskext.mysql import MySQL
app = Flask(__name__)
mysql = MySQL()
app.config['MYSQL_DATABASE_HOST '] = 'localhost'
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'mypassword'
app.config['MYSQL_DATABASE_DB'] = 'users'
app.config['MYSQL_CURSORCLASS'] = 'DictCursor'
mysql.init_app(app)
cur = mysql.connection.cursor()
@app.route('/')
def index():
return render_template('layout.html')
if __name__ == '__main__':
app.run(debug=True)
When I run this script in Git Bash, I get:
$ python app.py
Traceback (most recent call last):
File "app.py", line 13, in <module>
cur = mysql.connection.cursor()
AttributeError: 'MySQL' object has no attribute 'connection'
I do not see any issue with the app.py script above. The MySQL module is imported, initialized and it still does not run. Can someone please offer some suggestions of what could possibly be the problem?
from flask_mysqldb import MySQLinstead offrom flaskext.mysql import MySQL... Or use your previous import and use the cursos as:cur = mysql.get_db().cursor().