连接数据库并在localhost上显示结果

时间:2015-08-30 06:19:23

标签: python

我改变了代码如下

    from flask import Flask
from flaskext.mysql import MySQL

mysql = MySQL()
app = Flask(__name__)
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = ''
app.config['MYSQL_DATABASE_DB'] = 'OMTSL_QBO'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)

@app.route("/")
def hello():
    return "Welcome to Python Flask App!"

@app.route("/Authenticate")
def Authenticate():
    cursor = mysql.connect().cursor()
    cursor.execute("SELECT count(*) from REPORT_SUITE")
    data = cursor.fetchone()
    if data is None:
        print ("Database version : %s " % data)    
        return data
    else:
        return "Logged in successfully"

if __name__ == "__main__":
    app.run(debug = True)

但我仍然没有在浏览器窗口中看到打印消息。我想在本地主机

上运行时在浏览器上看到计数消息

1 个答案:

答案 0 :(得分:0)

如果您想返回实际查询结果,则应在return data count上使用def count(): # ... same print ("Database version : %s " % data) # return the database result from the 'count' function return data 语句:

def count():
    # ... create data variable
    mydata = "Database version : %s " % data
    # print data in console
    print mydata
    # return the datato caller (and then to user...)
    return mydata

修改:如果您想要完全相同的数据,请确保返回相同的数据:

<li class="quest" data-answer="Cystic hygroma">