In my Flask project, I want to select everything from a table in database and print each row in separate line?
How should I pass data through a for loop in this script below?
And how can I return it?
In app.py:
from flask import Flask, render_template
import MySQLdb
app = Flask(__name__)
@app.route('/')
def db():
db = MySQLdb.connect("localhost","myusername","mypassword","mydbname" )
cursor = db.cursor()
cursor.execute("SELECT * from p_user")
cursor.fetchall()
db.close()
return
if __name__ == '__main__':
app.run()
===============================================
Even this try didn't work.
I edited 2 lines in app.py like this:
data = str(cursor.fetchall())
return render_template('db.html', data = data)
And created db.html like this:
{% for each in data %}
print {{ each }}<br>
{% endfor %}
And the output was:
print (
print (
print 1
print ,
print
print '
print s
print h
....
There is a list of tuples, how I can reach each index.