[英]oracle query in python - The return type must be a string, dict, tuple
I'm trying to show the output of a query on the web api endpoint but it isn't working.我正在尝试在 web api 端点上显示查询的输出,但它不起作用。
The code below is returning TypeError: The view function did not return a valid response.下面的代码返回TypeError:视图函数没有返回有效的响应。 The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a int .
返回类型必须是字符串、字典、元组、响应实例或可调用的 WSGI,但它是一个 int 。
I already tried different ways but no luck.我已经尝试了不同的方法,但没有运气。 What am I missing here?
我在这里缺少什么?
from flask import Flask, jsonify, request, render_template
from flask_cors import CORS
import cx_Oracle
import os
import json
import logging
app = Flask(__name__)
app.logger.disabled = True
log = logging.getLogger('werkzeug')
log.disabled = True
@app.route('/query')
def query1():
connection = cx_Oracle.connect(user='superuser', password='mypass1', dsn='moon.my-org.local:1521/db1_sql')
cursor = connection.cursor()
cursor.execute("""SELECT * from users""")
result = cursor.fetchone()
if result == None:
return("No results")
exit
else:
while result:
return result[0]
result = cursor.fetchone()
cursor.close()
connection.close()
port = int(os.getenv("PORT"))
app.run(host='0.0.0.0', port=port, debug=True)
If the problem is just that you're returning an integer instead of a string, then it's a simple matter to transform it into a string with str()
:如果问题只是您返回的是整数而不是字符串,那么使用
str()
将其转换为字符串是一件简单的事情:
return str(result[0])
However, you have another problem -- that return statement is inside a loop, which means only the very first result will be returned and then the function will exit altogether.但是,您还有另一个问题——return 语句位于循环内,这意味着只会返回第一个结果,然后函数将完全退出。 The loop won't execute past the first iteration.
循环不会在第一次迭代之后执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.