Home > OS >  can't put variables inside sqlite3 query in flask
can't put variables inside sqlite3 query in flask

Time:12-27

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?

CodePudding user response:

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!
  • Related