Skip to content
Advertisement

can’t put variables inside sqlite3 query in flask

def query_db(query, args=(), one=False):
    cur = get_db().execute(query, args)
    rv = cur.fetchall()
    cur.close()
    return (rv[0] if rv else None) if one else rv

Like documentation says, and I tried:

query_db("INSERT INTO users (name, email, pass) VALUES (?, ?, ?)", (name, email, password))

it didn’t work, and I tried:

cur = get_db.cursor()
cur.execute("INSERT INTO users (name, email, pass) VALUES (?, ?, ?)", (name, email, password))

also didn’t work. How can I pass variables inside my request?

Advertisement

Answer

The insertion looks correct. However, until you perform a commit, any DML operation (like an insert) would only be visible from the session that performed it, and will implicitly be rolled back when the connection is terminated.

To make a long story short – call commit:

cur = get_db.cursor()
cur.execute("INSERT INTO users (name, email, pass) VALUES (?, ?, ?)", (name, email, password))
get_db.commit() # Here!
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement