【问题标题】:Is there is any solution for displaying sqlite database into my website using python flask?有没有使用 python 烧瓶将 sqlite 数据库显示到我的网站的解决方案?
【发布时间】:2019-12-13 17:14:28
【问题描述】:

我正在使用 python 和烧瓶创建用于获取 rss 提要的网络应用程序。我可以将提要存储在 sqlite 数据库中,但我无法在我的网站中显示它,我尝试了一些代码,但它没有路由到该网站,但应用程序正在运行并获取 rss 提要。请帮我解决这个问题

除了这些代码我不知道,请帮助我!

这是我正在使用的代码。

conn = sqlite3.connect('data.db')
c = conn.cursor()

def post_in_db(title):
    c.execute("SELECT * FROM feeds WHERE EXISTS(SELECT * FROM feeds WHERE title =?)",[title])
    exist = c.fetchone()
    if exist is None:
        return 0
    else:
        return 1

urllist = list()
count = 0
with open('urlList.txt') as fp:
    urllist = fp.read().splitlines()

def insertPost():
    for i in range(0,len(urllist)):
        parse_feed(urllist[i])

def parse_feed(url):
    feed= feedparser.parse(url)

    for post in feed.entries:
        if not post_in_db(post.title):
            data_entry(post.title, post.summary, post.link, post.published)

@app.route('/', defaults={'entries': 100})
def index():
    db_query = "select * from feeds limit %d" % entries
    res = db.engine.execute(db_query)
    return render_template("index.html", feeds=res.fetchall())

而我的 HTML 代码是

    <table>
        <tr>
            <th>title</th>
            <th>summary</th>
            <th>link</th>
            <th>datestamp</th>
        </tr>
        {% for user in feeds %}
            <tr>
                <td>{{ user[0] }}</td>
                <td>{{ user[1] }}</td>
                <td>{{ user[2] }}</td>
                <td>{{ user[3] }}</td>
              </tr>
        {% endfor %}
    </table>

如何在网站中路由和显示。

我希望将在 sqlite 数据库中获取的提要显示到我的网站

【问题讨论】:

  • 做一些调试来查明什么是不工作的。是读数据库吗?文件?在模板中显示数据?
  • 当我运行此代码时,它不会给出任何错误,我最终使用此代码 @app.route('/index.html') def get_from_db(): c.execute('SELECT * FROM feeds')返回render_template('index.html',rows = c.fetchall())但它不工作我使用shedule时间来更新feed当我使用这个代码它不工作并且我不使用它它定向到html页面,但它给出的网址未找到

标签: python sqlite flask


【解决方案1】:

我不知道为什么你在后端发送变量feeds 到视图而不是用户时为什么要执行{% for user in users %}

如果 SQL 查询按预期取回结果,您通常会使用它来显示数据。

{% for feed in feeds %}

{{ feed }} 

{% endfor %}

因此,通过查看您的后端代码,数据位于变量提要中。

根据您提供的新代码,请尝试如下路线:

@app.route("/")
@app.route("/index")
def index():
    con = sqlite3.connect('data.db')
    db = con.cursor()
    feeds = db.execute("SELECT * FROM feeds")
    feed=feeds.fetchall()
    conn.close()

    return render_template("index.html", feed=feed)

在你看来尝试:

{% for f in feed %}

{{ f }} 

{% endfor %}

【讨论】:

  • 我用用户作为提要进行了编辑,但它正在运行并显示 404 错误,但我做的一切都是正确的
  • 什么是显示 404 错误?你能告诉我们错误吗?
  • 未找到 在服务器上未找到请求的 URL。如果您手动输入了 URL,请检查您的拼写并重试。
  • 我正在获取 html 文件,但现在我无法在网站上获取数据库详细信息,这是我正在使用的代码,请帮助我! @app.route("/") @app.route("/index", methods=['GET']) def index(): con = sqlite3.connect('data.db') db = con.cursor( ) feeds = db.execute("SELECT * FROM feeds") return render_template("index.html", feed=feeds.fetchall()) c.close() conn.close()
  • 你能试试这个代码吗?(检查答案)或者更好的是你的完整应用程序被推到一个我可以审查和建议更好的地方吗?自从对此发表新评论后,我已经编辑了我的答案
猜你喜欢
  • 2013-07-07
  • 2021-10-27
  • 2021-04-11
  • 2022-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-17
相关资源
最近更新 更多