0

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'))
1
  • Which line is giving you the error, could you please update your question with a complete stack trace? Commented Sep 2, 2017 at 21:42

1 Answer 1

2

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])
Sign up to request clarification or add additional context in comments.

5 Comments

that gives me sqlite3.InterfaceError: Error binding parameter 1 - probably unsupported type.
Use print(request.form['chapter']) to make sure it isn't None. Make sure all your form data is coming back correctly.
@nasan This question fixed your problem with the 3 arguments error so you should mark it correct. The new error you're getting is unrelated (it's something to do with the actual data you're passing). You should google your new problem and return to SO with a new question if you can't solve it.
I changed book_id to str(book_id). I can see the values in my table now. chapter is the int that I insert into the html form, but book_id comes out like this <sqlite3.Row object at 0x102755d10>
It's saying that it's having trouble binding parameter 1 which is the chapter. If your chapter is an int in the database then you'll need to convert the form value to an integer since it will come in as a string. Likewise, if the book_id is an int in the database you shouldn't convert it to a string.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.