0

I'm having problems printing contents of SQLite database into a Flask web page.

The output of the code below does not return the records I have fetched from the database into the Flask web page which is expecting it, and will display into a table.

The code I have so far:

flask.py

from flask import Flask 
from flask import render_template 
import sqlite3

conn = sqlite3.connect('sqlite_test.db') c = conn.cursor()
app = Flask(__name__)

@app.route('/') def index():    
 c.execute('SELECT * FROM facebook_posts')    
 return render_template('flask.html', rows = c.fetchall())

if __name__ == '__main__':
    app.run()

flask.html

<table class="table table-hover">
        <thead>
          <tr>
            <th>StatusID</th>
            <th>Status Message</th>
            <th>Link</th>
            <th>Status Type</th>
            <th>Status Link</th>
            <th>Status Published</th>
            <th>Reaction Count</th>
            <th>Comment Count</th>
            <th>Share Count</th>
            <th>Like Count</th>
            <th>Love Count</th>
            <th>Wow Count</th>
            <th>Haha Count</th>
            <th>Sad Count</th>
            <th>Angry Count</th>          
          </tr>
                  </thead>
                  <tbody>
                    {% for row in rows %}
                  <tr>
                    <td>{{status_id}}</td>
                    <td>{{status_message}}</td>
                    <td>{{link_name}}</td>
                    <td>{{status_type}}</td>
                    <td>{{status_link}}</td>
                    <td>{{status_published}}</td>
                    <td>{{num_reactions}}</td>
                    <td>{{num_comments}}</td>
                    <td>{{num_shares}}</td>
                    <td>{{num_likes}}</td>
                    <td>{{num_loves}}</td>
                    <td>{{num_wows}}</td>
                    <td>{{num_hahas}}</td>
                    <td>{{num_sads}}</td>
                    <td>{{num_angrys}}</td>
                  </tr>
                    {% endfor %}
                  </tbody>
              </table>

Am I missing something or might have overlooked?

2
  • Your filename is flask.html and you are using index.html. Commented Feb 24, 2017 at 5:27
  • @KlausD Hey, thanks! I did not notice that. Unfortunately, I have changed the filename already but it did not solve the problem. Commented Feb 24, 2017 at 5:36

1 Answer 1

2

And at index.html, if status_id and status_message ... are you SQLite column, you should use row.status_id and row.status_message and so on.

Sign up to request clarification or add additional context in comments.

3 Comments

Hey, thank you, your suggestion solved part of my problem. But it didn't work right away. I have to change some code in my flask.py file.
Oh, your flask.py may: cur = c.execute('SELECT * FROM facebook_posts') and then rows = cur.fetchall(). By the way, I think you should read document firstly.
It turns out that I have to put the data into a dictionary first iterate through it and use the fetchall() function then put the data inside a variable before passing it to the view. I now have solved it. Again, thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.