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.

Asked By: Itrock

||

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] )
Answered By: Tim Roberts
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.