2016-05-26 127 views
0

我想下面的代碼顯示在Web瀏覽器的簡單查詢的結果顯示查詢結果,但我得到一個500錯誤嘗試使用燒瓶中使用Pything

from flask import Flask 
import sql_class 

app = Flask(__name__) 

@app.route("/") 
def hello(): 
    try: 
     sql = sql_class.executeScriptsFromFile 
     result = sql("My random file path") 
     print "it works" 
     return result 
    except: 
     print "you broke it" 
     return "hello world" 

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

的sql_class是一個自定義我在常規基礎上使用的類連接到我們的SQL服務器數據庫,並從給定的文件路徑運行腳本。我可以將代碼複製功能之外,打印結果到控制檯窗口就好了,但由於某種原因它燒瓶函數中分解

+2

什麼是查詢返回? (result變量的值是多少) – noteness

+2

當你設置app.debug = True時,它在瀏覽器中顯示的是什麼? – nephlm

+0

@ShamilKMuhammed該查詢實際上是「按城市從table1組中選擇城市」。它在答案集中有8條記錄。 – John

回答

0

您的查詢返回一個列表,你應該不會返回列表中瓶響應。

嘗試使你的列表的字符串,像這樣:

@app.route("/") 
def hello(): 
    try: 
     sql = sql_class.executeScriptsFromFile 
     result = sql("My random file path") 
     print "it works" 
     return str(result) 
    except: 
     print "you broke it" 
     return "hello world" 

或者其編碼爲JSON:

import json 

@app.route("/") 
def hello(): 
    try: 
     sql = sql_class.executeScriptsFromFile 
     result = sql("My random file path") 
     print "it works" 
     return json.dumps(result) 
    except: 
     print "you broke it" 
     return "hello world" 

或使其逗號分隔字符串,像這樣:

@app.route("/") 
def hello(): 
    try: 
     sql = sql_class.executeScriptsFromFile 
     result = sql("My random file path") 
     print "it works" 
     return ", ".join(result) 
    except: 
     print "you broke it" 
     return "hello world" 

我建議服用燒瓶教程

+0

顯示信息這是完美的!謝謝:) – John

+0

歡迎:) – noteness