简体   繁体   中英

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.

So in my code python code I am trying to collect the data from the customer feedback form and hold it in a database.

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.

my python flask code:

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.

I can upload the html file on request; I'm not too sure if I have space to do so along with my python file.

I think that this is due to your line conn.commit() in your process() function. 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). 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

I can edit my answer if you provide more context regarding where your code is failing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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