Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Inserting into database, incorrect number of bindings supplied (Flask, SQL, Python)

Hello Treehousers:

I am trying to make a simple messageboard/twitter type website. It contains a text entry input and a post button. You type something in, hit post, and the page refreshes with your post added to the top.

I have this setup in Flask framework and am having trouble posting the entries into the database. (I can read them just fine if I insert using sqlite browser). The database is a table called entries and each entry has a row that contains an autoincrement ID and the text from the entry field.

The problem I am having is that when I try to insert into the database via POST it sees the characters of the text entered as individual entries. For example if I enter 12345 it will try to post 1,2,3,4,5 separately. See the pictures for a better explanation

Entering text http://drp.io/obD

Traceback, incorrect number of bindings http://drp.io/obC

If I enter just 1 character, it works perfectly. I'm fairly certain it has something to do with the way I am adding to the database. Here is my query (couldn't get it to format)

g.db.execute('INSERT INTO entries (text) VALUES (?)', request.form['posts'])

(posts is the name of the textarea)

Hopefully I just missing a comma or something. I can provide more code if needed.

Thanks in advance for any replies!

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Definitely share the code around your entry creation. Also, I'd suggest looking into using an ORM instead of writing raw SQL. Peewee is a great choice and we have a course over it already.

Could you share some more of the code around the SQL?