使用Python / Flask / MySQL实现数据库连接状态时遇到问题

时间:2017-06-20 02:09:05

标签: python mysql flask database-connection pythonanywhere

我正在PythonAnywhere平台上使用Python / Flask / MySQL构建一个网站。我之前已经使用过这个数据库了(读取:数据库连接不是这里的问题,它是设置/配置和调用),但我想将其更改为更符合Web开发(即应用程序/请求)上下文)。

根据文档和其他帖子(例如:Connect to a Database in Flask, Which Approach is better?),我几乎没有尝试过不同的配置。虽然提供了非常丰富的信息,但我仍然无法成功实施,并希望有人能指出它为什么不起作用。

我正在努力保持简单,所以暂时将所有内容放在一个MyApp.py文件中,如下所示:

from flask import Flask, render_template, session, request, redirect, url_for, g
from flask.ext.mysql import MySQL

app = Flask(__name__, static_url_path='')
app.config["DEBUG"] = True

def get_db():
    db = getattr(g, '_database', None)
    if db is None:
        db = g._database = connect_to_database()
    return db

@app.teardown_appcontext
def teardown_db(exception):
    db = getattr(g, '_database', None)
    if db is not None:
        db.close()

def connect_to_database():
    mysql = MySQL()
    app.config['MYSQL_DATABASE_USER'] = '{db_user}'
    app.config['MYSQL_DATABASE_PASSWORD'] = '{db_password}'
    app.config['MYSQL_DATABASE_DB'] = '{db}'
    app.config['MYSQL_DATABASE_HOST'] = '{dbhost}'
    dbcon = mysql.connect()
    return dbcon

@app.route('/', methods=["GET", "POST"])
def index():
    db_conn = get_db()
    cursor = db_conn.cursor()
    #... do my SQL stuff here ...#
    return render_template("home_guest.html")

它到达mysql.connect()函数时失败,我无法解决原因。以下是Python Anywhere服务器的错误日志:

2017-06-20 02:03:23,228: Error running WSGI application
2017-06-20 02:03:23,230: AttributeError: 'NoneType' object has no attribute 'config'
2017-06-20 02:03:23,230:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2000, in __call__
2017-06-20 02:03:23,230:     return self.wsgi_app(environ, start_response)
2017-06-20 02:03:23,231: 
2017-06-20 02:03:23,231:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1991, in wsgi_app
2017-06-20 02:03:23,231:     response = self.make_response(self.handle_exception(e))
2017-06-20 02:03:23,231: 
2017-06-20 02:03:23,231:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1567, in handle_exception
2017-06-20 02:03:23,232:     reraise(exc_type, exc_value, tb)
2017-06-20 02:03:23,232: 
2017-06-20 02:03:23,232:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app
2017-06-20 02:03:23,232:     response = self.full_dispatch_request()
2017-06-20 02:03:23,232: 
2017-06-20 02:03:23,232:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1641, in full_dispatch_request
2017-06-20 02:03:23,232:     rv = self.handle_user_exception(e)
2017-06-20 02:03:23,232: 
2017-06-20 02:03:23,232:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1544, in handle_user_exception
2017-06-20 02:03:23,233:     reraise(exc_type, exc_value, tb)
2017-06-20 02:03:23,233: 
2017-06-20 02:03:23,233:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1639, in full_dispatch_request
2017-06-20 02:03:23,233:     rv = self.dispatch_request()
2017-06-20 02:03:23,233: 
2017-06-20 02:03:23,233:   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1625, in dispatch_request
2017-06-20 02:03:23,233:     return self.view_functions[rule.endpoint](**req.view_args)
2017-06-20 02:03:23,233: 
2017-06-20 02:03:23,233:   File "/home/WazzalJohn/mysite/flask_app.py", line 83, in login
2017-06-20 02:03:23,234:     conn = get_db()
2017-06-20 02:03:23,234: 
2017-06-20 02:03:23,234:   File "/home/WazzalJohn/mysite/flask_app.py", line 15, in get_db
2017-06-20 02:03:23,234:     db = g._database = connect_to_database()
2017-06-20 02:03:23,234: 
2017-06-20 02:03:23,234:   File "/home/WazzalJohn/mysite/flask_app.py", line 40, in connect_to_database
2017-06-20 02:03:23,234:     dbcon = mysql.connect()
2017-06-20 02:03:23,234: 
2017-06-20 02:03:23,235:   File "/home/WazzalJohn/.local/lib/python2.7/site-packages/flaskext/mysql.py", line 39, in connect
2017-06-20 02:03:23,235:     if self.app.config['MYSQL_DATABASE_HOST']:

任何建议或帮助将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:2)

您必须调用init_app来初始化应用程序以用于MySQL类。然后从扩展程序访问连接。这不是一个功能。

mysql = MySQL()
app.config['MYSQL_DATABASE_USER'] = '{db_user}'
app.config['MYSQL_DATABASE_PASSWORD'] = '{db_password}'
app.config['MYSQL_DATABASE_DB'] = '{db}'
app.config['MYSQL_DATABASE_HOST'] = '{dbhost}'
mysql.init_app(app)

@app.route('/')
def index():
    c = mysql.connection.cursor()
    ...