Reputation: 93
I'm trying to insert data into a table from a form and from a different table, but I'm getting this error.
TypeError: function takes at most 2 arguments (3 given)
db.execute("INSERT INTO chapters (chapter, book_id) VALUES (?, ?)", [request.form['chapter']], book_id)
@app.route('/newbook')
def new_book():
return render_template('newbook.html')
@app.route('/addbook', methods=['GET', 'POST'])
def addbook():
db = get_db()
db.execute("INSERT INTO books (title) VALUES (?)",
[request.form['title']])
db.commit()
return redirect(url_for('new_chapter'))
@app.route('/newchapter')
def new_chapter():
return render_template('newchapter.html')
@app.route('/addchapter', methods=['GET', 'POST'])
def addchapter():
db = get_db()
cur = db.execute("SELECT last_insert_rowid()")
book_id = cur.fetchone()
db.execute("INSERT INTO chapters (chapter, book_id) VALUES (?, ?)", [request.form['chapter']], book_id)
db.commit()
return redirect(url_for('new_concepts'))
Upvotes: 0
Views: 177
Reputation: 91525
The error is telling you what's wrong, you've given the following line 3 arguments when it only can accept 2.
db.execute("INSERT INTO chapters (chapter, book_id) VALUES (?, ?)", [request.form['chapter']], book_id)
The 1st argument is supposed to be the SQL query while the 2nd argument is supposed to be a list of values that will replace the ?
placeholders. However, you've accidentally put the book_id
outside of the list, causing it to be passed as a 3rd argument. It should be inside the list as follows:
db.execute("INSERT INTO chapters (chapter, book_id) VALUES (?, ?)", [request.form['chapter'], book_id])
Upvotes: 2