我正在尝试使用以下代码在网络浏览器中显示简单查询的结果,但收到500错误
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 Server DB并从给定的文件路径运行脚本。我可以将函数复制到函数外部并将结果打印到控制台窗口就好了,但由于某种原因它在烧瓶函数中分解
答案 0 :(得分:0)
您的查询返回一个列表,您不应该在Flask中将列表作为响应返回。
尝试将列表设为字符串,如下所示:
@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"
我建议参加Flask教程