Insert into database fails
Question:
My code:
@app.route('/projects/create', methods=["POST", "GET"])
def add():
if request.method == "POST":
id = request.form["id"]
name = request.form["name"]
text = request.form["s_text"]
try:
projects.db.executemany("INSERT INTO projects (?,?,?)", [id], [name], [text])
projects.db.commit()
return redirect("/projects")
except:
return "Error 404"
else:
return render_template("create.html")
I’m trying to insert into database but nothing worked.
Answers:
You don’t have many rows, you have one. You need to use the VALUES keyword to signal that values are coming. And your three fields need to be ONE list. You need
projects.db.execute("INSERT INTO projects VALUES (?,?,?)", [id, name, text] )
My code:
@app.route('/projects/create', methods=["POST", "GET"])
def add():
if request.method == "POST":
id = request.form["id"]
name = request.form["name"]
text = request.form["s_text"]
try:
projects.db.executemany("INSERT INTO projects (?,?,?)", [id], [name], [text])
projects.db.commit()
return redirect("/projects")
except:
return "Error 404"
else:
return render_template("create.html")
I’m trying to insert into database but nothing worked.
You don’t have many rows, you have one. You need to use the VALUES keyword to signal that values are coming. And your three fields need to be ONE list. You need
projects.db.execute("INSERT INTO projects VALUES (?,?,?)", [id, name, text] )