Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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?