title: (flask) python mysql - how to pass selected data though a for loop and return it?
body: <p>In my Flask project, I want to select everything from a table in database and print each row in separate line?</p>
  
  <p>How should I pass data through a for loop in this script below?</p>
  
  <p>And how can I return it?</p>
  
  <p>In app.py:</p>
  
  <pre><code>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()
  </code></pre>
  
  <p>===============================================</p>
  
  <p>Even this try didn't work.</p>
  
  <p>I edited 2 lines in app.py like this:</p>
  
  <pre><code>data = str(cursor.fetchall())
  
  return render_template('db.html', data = data)
  </code></pre>
  
  <p>And created db.html like this:</p>
  
  <pre><code>{% for each in data %}
  
      print {{ each }}&lt;br&gt;
  
  {% endfor %}
  </code></pre>
  
  <p>And the output was:</p>
  
  <pre><code>print (
  print (
  print 1
  print ,
  print
  print '
  print s
  print h
  ....
  </code></pre>
  
  <p>There is a list of tuples, how I can reach each index.</p>