【发布时间】:2018-01-13 06:26:32
【问题描述】:
我正在尝试建立与 sql 数据库的连接,但我最终得到了
AttributeError: 'MySQL' object has no attribute 'connection'
我已经阅读了文档,看起来这是创建游标的正确方法:
from flask import Flask
from flaskext.mysql import MySQL
app = Flask(__name__)
app.config['MYSQL_HOST'] = 'sql3.freemysqlhosting.net'
app.config['MYSQL_USER'] = 'sql1234567'
app.config['MYSQL_PASSWORD'] = 'abcdefg'
app.config['MYSQL_DB'] = 'sql1234567'
app.config['MYSQL_PORT'] = '3306'
mysql = MySQL(app)
@app.route('/')
def index():
cur = mysql.connection.cursor()
cur.execute('''SELECT * FROM users''')
rv = cur.fetchall()
return str(rv)
if __name__ == '__main__':
app.run(debug=True)
编辑:使用https://www.youtube.com/watch?v=nWO_VpI5wn8 作为教程,文档为http://flask-mysqldb.readthedocs.io/en/latest/
【问题讨论】: