First Flask project – database file won’t update

I started learning Flask recently and the tutorial I’m following wants me to set up a To Do list application using sqlite3 as the database to store username, password, and the to do list itself. So far, I have my Schema and Models set up exactly like the tutorial, but my database file isn’t updating. My tables show up in the database file, but they are empty. What am I doing wrong?

This is the schema:

import sqlite3

connection = sqlite3.connect('to_do_list.db', check_same_thread = False)
cursor = connection.cursor()

cursor.execute(
    """CREATE TABLE users(
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        username VARCHAR(32),
        password VARCHAR(32)    
    );"""
)

connection.commit()
cursor.close()
connection.close()

Here is the signup model:

def signup(username, password):
connection = sqlite3.connect('to_do_list.db', check_same_thread = False)
    cursor = connection.cursor()
    cursor.execute( """SELECT password FROM users WHERE username = '{username}';""".format(username = username))
    exist = cursor.fetchone()

    if exist is None:
        cursor.execute("""INSERT INTO users(username, password)VALUES('{username}', '{password}');""".format(username = username, password = password))            
            
        connection.commit()
        cursor.close()
        connection.close()

    else:
        return ('User already exists!')

Here is the Flask routing:

@app.route('/signup', methods = ['GET', 'POST'])
def signup():
    if request.method == 'GET':
        message = 'Please sign up!'
        return render_template('signup.html', message = message)
    else:
        username = request.form['username']
        password = request.form['password']
        return render_template('todo.html')

Answer

I decided to sleep on it and came up with the solution. The tutorial wasn’t clear on some of the functions, and John Gordon’s above comment helped me figure it out. I had to call model code in the route function. I also had to ‘import model’ since my models are in a different .py file.

The new routing:

@app.route('/signup', methods = ['GET', 'POST'])
def signup():
    if request.method == 'GET':
        message = 'Please sign up!'
        return render_template('signup.html', message = message)
    else:
        username = request.form['username']
        password = request.form['password']
        db = model.signup(username, password)
        return render_template('todo.html')

Thanks for your input everyone! Be healthy and safe, and happy holidays.