繁体   English   中英

Flask Framework中的数据库连接问题

[英]Issues with DB connections in Flask Framework

我是Flask框架的新手,我刚刚在Flask框架中创建了应用程序,但现在我在Flask中与DB连接进行挣扎。 我想将我的应用程序与MySQL连接。 为此,我点击了此链接http://flask.pocoo.org/snippets/11/ ,但是我无法与DB连接。 我的代码如下:

from flask import Flask , render_template,g
from torndb import Connection

app=Flask(__name__)


@app.before_request
def connect_db():
    g.db = Connection(DB_HOST="localhost", 
                      DB_NAME="flask",
                      DB_USER="root",
                      DB_PASSWD="ghrix321")


@app.route('/')
def home():
    rows = g.db.iter("select * from user")
    return render_template('home.html',rows=rows)

TypeError: init ()获得了意外的关键字参数“ DB_NAME”。

因此,请以某种方式建议我,以便我可以与DB连接并从那里获取数据。 谢谢

您引用的代码段不使用关键字参数。

torndb的文档位于http://torndb.readthedocs.org/en/latest/ 如果使用关键字参数,则必须像在函数定义中一样对其进行命名。 这是正确的调用:

g.db = Connection('localhost','flask', user='root', password='ghrix321')

顺便说一句,在数据库中使用专用用户,不要将密码硬编码到应用程序中,而应使用配置文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM