如何在Flask中从SQLite数据库打印数据

时间:2017-02-24 05:19:54

标签: python sqlite flask

我在将SQLite数据库的内容打印到Flask网页时遇到问题。

以下代码的输出不会将我从数据库中提取的记录返回到期待它的Flask网页,并将显示在表格中。

到目前为止我的代码:

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>

我错过了什么或者可能被忽视了吗?

1 个答案:

答案 0 :(得分:1)

index.html,如果status_id和status_message ...是SQLite列,则应使用row.status_idrow.status_message,依此类推。