Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (19.9k points)

I'm working with Python and Flask for the first time. I'm trying to create a simple query select to my database and display the data.

from flask import Flask

from flaskext.mysql import MySQL

mysql = MySQL()

app = Flask(__name__)

app.config['MYSQL_DATABASE_USER'] = 'myuser'

app.config['MYSQL_DATABASE_PASSWORD'] = 'mypass'

app.config['MYSQL_DATABASE_DB'] = 'mydb'

app.config['MYSQL_DATABASE_HOST'] = 'my server'

mysql.init_app(app)

@app.route("/")

def hello():

    cursor = mysql.connect().cursor()

    cursor.execute("Select * From Users Where UserID = 215")

    data = cursor.fetchone()

    return data

if __name__ == "__main__":

    app.run()

It is giving me the error: The view function did not return a valid response tuple.

Thanks

1 Answer

0 votes
by (25.1k points)

You can use json.dumps method for this.

First import json and then instead of returning data return json.dumps(data)

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 2, 2020 in Python by Sudhir_1997 (55.6k points)
0 votes
1 answer
asked Jul 2, 2020 in Python by Sudhir_1997 (55.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...