I'm making a simple registration page in Flask using sqlite. I did a test run manually and this worked:
import sqlite3
db = sqlite3.connect('database.db')
c = db.cursor()
username = "bb"
password = "vv"
c.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password))
db.commit()
But I can't get this to work in my app.py (below is a greatly simplified version)
@app.route("/register", methods=["GET", "POST"])
def register():
if request.method == "POST":
username = request.form.get("username")
password = request.form.get("password")
confirmation = request.form.get("confirmation")
try:
c.execute("INSERT INTO users (username, password) VALUES (?, ?)", (username, password))
db.commit()
return render_template("login.html")
except:
return apology("username is already registered")
else:
return render_template("register.html")
I get redirected to my apology page (Username is already registered). My database.db is empty :( Any help would be appreciated, new to the community and coding.
CodePudding user response:
The issue: "SQLite objects created in a thread can only be used in that same thread."
I just added the cursor and now it works
c = c = db.cursor()