Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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!
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading