Home » Python » error sending data to sqlite3 database with python/flask-Exceptionshub

error sending data to sqlite3 database with python/flask-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment


I’m making a small registration site for the registration of guests.
I’m using a sqlite3 database.

When I run my code I get the error “sqlite3.OperationalError: no such table: guests”
But the weird thing is that there is a table named guests in the database when I look it up in the terminal, see screenshot.

enter image description here

The name of the database is “gast.db”
Can someone see what I’m doing wrong?

here is my Python/flask code:

from flask import Flask, render_template, request
import sqlite3
from sqlite3 import Error

app = Flask(__name__)

# Here I create a DB and a connection
def create_connection(db_file):
    conn = None
        conn = sqlite3.connect(db_file)

    except Error as e:
    return conn

# here I insert the created tab;es
def create_table():
    database = r"gast.db"

    table = """ CREATE TABLE IF NOT EXISTS guests (
                                        id integer PRIMARY KEY,
                                        voor_naam text NOT NULL,
                                        achter_naam text NOT NULL,
                                        bedrijfs_naam text NOT NULL,
                                        datum date NOT NULL

                                    ); """

    conn = create_connection(database)

def create_guest(conn, guests):
    sql = ''' INSERT INTO guests(voor_naam,achter_naam,bedrijfs_naam,datum)
              VALUES(?,?,?,?) '''

    cur = conn.cursor()
    cur.execute(sql, guests)
    return cur.lastrowid

# index for the html page
def index():
    return render_template("index.html")

# sending input form form to db
@app.route('/my_form', methods=['POST'])
def my_form():
    database = r"gast.db"

    if request.method == 'POST':
        conn = create_connection(database)
        with conn:

            guest_vnaam = request.form.get('Voornaam')
            guest_anaam = request.form.get('Achternaam')
            guest_cnaam = request.form.get('Bedrijfsnaam')
            guest_datum = request.form.get('Datum')

            if conn is not None:
                guest = (guest_vnaam, guest_anaam, guest_cnaam, guest_datum)
                create_guest(conn, guest)
                return render_template("index.html")

                return 'oops, er is iets verkeerd gegaan...'

# This is where I run the app 
if __name__ == '__main__':

The error says something is going wrong when calling the “create_guest” function/method.
If someone could help me that would be great!

Thanks in advance

How to&Answers:

I had a similar error to this a while back. I am not 100% sure that this is correct for you however the error is that SQLite3 is single threaded and flask is multi threaded. Therefore when a thread of flask is asking for data from SQLite3 and you get other threads doing the same thing you get weird errors. In order to fix this is swapped over to using something that is multi-threaded like MongoDB or sqlalchemy, which there is a flask extension for called flask sqlalchemy