简体   繁体   中英

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.

The code below is returning TypeError: The 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 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() :

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. The loop won't execute past the first iteration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM