【问题标题】:How can I connect my flask app to my SQLite3 database?如何将我的烧瓶应用程序连接到我的 SQLite3 数据库?
【发布时间】:2019-01-27 18:48:35
【问题描述】:

您好,我正在尝试创建客户反馈表;我已经设法创建了我需要的页面,但是我在将我的应用程序连接到我的 SQLite3 数据库时遇到了困难。

所以在我的代码 python 代码中,我试图从客户反馈表中收集数据并将其保存在数据库中。

在反馈表中,系统会提示他们输入自己的姓名,从下拉框中选择一些答案,并在最后写评论。

答案将保存在数据库中(供将来参考 - 例如报告等),用户将被重定向回主页,在那里他们将能够看到他们的姓名和评论(取自反馈表)。

我看过关于 sqlite3 的教程,这些教程很容易理解和执行(对我来说比 MySQL 容易得多),但我遗漏了一些东西,因为它无法连接到我的数据库。

我的python烧瓶代码:

from flask import Flask, render_template, redirect, url_for, request, session, flash, g
from functools import wraps
import sqlite3

app = Flask(__name__)
app.secret_key = "random_character_generator" # this would be random or anything the developer wants
app.database = "gymdatabase.db"

conn = sqlite3.connect(app.database)
c = conn.cursor()

def connect_db():
    return sqlite3.connect(app.database)

@app.route('/')
def home():
    g.db = connect_db()
    cur = g.db.execute('select * from posts')
    posts = [dict(name=row[0], welcome=row[1], equipment=row[2], cleanliness=row[3], interaction=row[4], comments=row[5], contact=row[6]) for row in cur.fetchall()]
    g.db.close()
    return render_template('gym_index.html', posts=posts)

@app.route('/feedback', methods=['POST'])
def feedback():
    return render_template('gym_feedback.html')

@app.route('/process', methods=['GET', 'POST'])
def process():
    g.db = connect_db()
    name = request.form['name']
    welcome = request.form['welcome']
    equipment = request.form['equipment']
    cleanliness = request.form['cleanliness']
    interaction = request.form['interaction']
    comment = request.form['comment']
    contact = request.form['yes_no']
    conn.commit()
    cur = g.db.execute(select * from posts)
    posts = [dict(name=row[0], welcome=row[1], equipment=row[2], cleanliness=row[3], interaction=row[4], comments=row[5], contact=row[6]) for row in cur.fetchall()]
    g.db.close()
    return redirect(url_for('home', posts=posts))   

当我尝试提交反馈表时,我得到: sqlite3.ProgrammingError: 在一个线程中创建的 SQLite 对象只能在同一个线程中使用。

我可以根据要求上传 html 文件;我不太确定我是否有空间与我的 python 文件一起这样做。

【问题讨论】:

  • 你是什么意思,“它不会连接”?发生什么了?你得到什么错误? (注意,在这段代码中,您实际上没有向数据库中插入任何内容。)

标签: python database flask sqlite


【解决方案1】:

我认为这是由于您的 conn.commit() 行在您的 process() 函数中。您在 Flask 首次启动时声明了 conn = sqlite3.connect(app.database),但是使用 @app.route(...) 函数装饰器定义的每个函数都会在不同的线程中调用以响应 HTTP 请求(如上述函数装饰器中所定义)。你可能想做这样的事情:

@app.route('/process', methods=['GET', 'POST'])
def process():
    ...
    db = connect_db()
    cur = db.cursor()
    cur.execute("select * from posts")
    results = cur.fetchall()
    ...

您可以查看此链接以获取更多文档:https://docs.python.org/2/library/sqlite3.html

如果您提供有关代码失败位置的更多上下文,我可以编辑我的答案。

【讨论】:

  • 谢谢我试过你的建议它仍然没有连接到我的数据库(传递的信息没有发送到我的数据库)。当我按下提交按钮时,它会重定向到主页,但没有任何内容发布或存储到数据库中。
  • 如果您要对数据库进行某种更新,则需要在您的cur.execute(...) 调用之后进行cur.commit() 调用。在我的示例中,您所做的只是从数据库中检索每一行并将其存储在 results 变量中。
  • 感谢巴勃罗;我设法创建了一个名为 posts 的空表,一旦用户填写了他们的反馈表,数据就应该被发送到数据库。只是用户的名字和他们的 cmets 应该从数据库中取出并在开始时重定向到主页。
  • 如果可能的话,我可以向您发送/发送电子邮件给您,也许您可​​以看到我可能出错的地方..
猜你喜欢
  • 1970-01-01
  • 2021-12-28
  • 1970-01-01
  • 2015-04-13
  • 2019-07-19
  • 1970-01-01
  • 1970-01-01
  • 2016-09-20
  • 2014-11-15
相关资源
最近更新 更多