can't put variables inside sqlite3 query in flask

Question:

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?

Asked By: Anatoliy

||

Answers:

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!
Answered By: Mureinik
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.