Pfigz
Pfigz

Reputation: 41

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

Upvotes: 2

Views: 128

Answers (2)

Pfigz
Pfigz

Reputation: 41

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.

Upvotes: 2

Federico Baù
Federico Baù

Reputation: 7705

here

import sqlite3

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

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

curr.commit()
curr.close()

You are adding the commit() function to the connection variable and not to the cursor.

Edit, here too:

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

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

else:
    return ('User already exists!')

IMPORTANT if there is a to_do_list.db already created, after the editing of the script, remember to delete the file so it will create a new one.

Upvotes: 1

Related Questions