简体   繁体   English

如何将烧瓶应用程序连接到SQLite3数据库?

[英]How can I connect my flask app to my SQLite3 database?

Hi I am trying to create a customer feedback form; 嗨,我正在尝试创建客户反馈表; I have managed to create the pages I need, but I am having difficulty connecting my app to my SQLite3 database. 我已经成功创建了所需的页面,但是在将应用程序连接到SQLite3数据库时遇到了困难。

So in my code python code I am trying to collect the data from the customer feedback form and hold it in a database. 因此,在我的代码python代码中,我试图从客户反馈表单中收集数据并将其保存在数据库中。

In the feedback form they will be prompted to input their name, choose some answers from a drop-box selection, and to write a comment at the end. 在反馈表中,将提示他们输入名称,从下拉列表框中选择一些答案,并在最后写评论。

The answers will be housed in the database (for future reference - like reports etc) and the user will be redirected back to the home page where they will be able to see their name & comment (taken from the feedback form). 答案将存储在数据库中(以供将来参考,例如报告等),并且用户将被重定向回首页,在那里他们将能够看到其姓名和评论(从反馈表中获取)。

I have watched tutorials on sqlite3 which was kind of easy to understand & execute (a lot easier for me than MySQL) but I'm missing something because it won't connect to my database. 我看过关于sqlite3的教程,该教程很容易理解和执行(对我来说,比MySQL容易得多),但是我缺少了一些东西,因为它无法连接到我的数据库。

my python flask code: 我的python flask代码:

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))   

When I try to submit a feedback form I get: sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. 当我尝试提交反馈表单时,我得到:sqlite3.ProgrammingError:在线程中创建的SQLite对象只能在同一线程中使用。

I can upload the html file on request; 我可以根据要求上传html文件; I'm not too sure if I have space to do so along with my python file. 我不太确定我是否有空间与python文件一起使用。

I think that this is due to your line conn.commit() in your process() function. 我认为这是由于您的process()函数中的conn.commit()conn.commit() You declare conn = sqlite3.connect(app.database) when Flask first starts, but each function defined with the @app.route(...) function decorator gets called in a different thread in response to HTTP requests (as defined in the aforementioned function decorator). 当Flask第一次启动时,您声明conn = sqlite3.connect(app.database) ,但是使用@app.route(...)函数修饰器定义的每个函数都会在不同的线程中调用,以响应HTTP请求(如上述功能装饰器)。 You probably want to do something like this: 您可能想要执行以下操作:

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

You can see this link for further documentation: https://docs.python.org/2/library/sqlite3.html 您可以看到此链接以获取更多文档: https : //docs.python.org/2/library/sqlite3.html

I can edit my answer if you provide more context regarding where your code is failing. 如果您提供有关代码失败位置的更多上下文,则可以编辑我的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM