简体   繁体   English

python中的oracle查询-返回类型必须是字符串、字典、元组

[英]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.

相关问题 类型错误:返回类型必须是字符串、字典、元组、响应实例或可调用的 WSGI,但它是一个列表 - Type Error: The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a list 返回类型必须是字符串、字典、元组、响应实例或可调用的 WSGI,但它是一个 TypeError - The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a TypeError TypeError:返回类型必须是字符串、字典、元组、响应实例或 WSGI 可调用的,但它是协程 - TypeError: The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a coroutine 查看 function 未返回有效响应。 返回类型必须是字符串、字典、元组、响应实例或 WSGI 可调用,但它是一个列表 - view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a list 类型错误:视图 function 未返回有效响应。 返回类型必须是字符串、字典、元组、Response 实例,但它是一个 int - TypeError: The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, but it was a int 错误:Spider 必须返回 Request、BaseItem、dict 或 None,得到 'tuple' - ERROR: Spider must return Request, BaseItem, dict or None, got 'tuple' 在python中输入“string”到“dict” - type "string" to “dict” in python 无法处理参数:int(1),它必须是 list、tuple 或 dict 类型 - Could not process parameters: int(1), it must be of type list, tuple or dict 为什么将Python dict转换为元组会返回一组键? - Why does casting a Python dict to a tuple return a tuple of keys? Python文件字符串替换字典和元组 - Python File String Replace Dict and Tuple
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM