1

Trying to learn Sqlite and I'm not sure I understand why I can't get this code to work:

def get_bday(self):
    name = self.input_name()
    self.c.execute('SELECT * FROM birthdays WHERE name =?', name)
    for row in self.c.fetchall():
        print(row)

The name variable is being returned from another method. For this example, I am using "joe smoe" without the quotes as the name variable to perform the query with. When I run the above code I get:

self.c.execute('SELECT * FROM birthdays WHERE name =?', name)
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 8 supplied.

The word "joe smoe" is 8 bindings long if you count the space. But I have no idea what that means. I assumed I could simply pass a variable right to Sqlite just as easily as I pass variables around in Python but that doesn't appear to be the case. I think it has something to do with my very poor understanding of tuples.

1 Answer 1

2

SQLite is currently thinking you want to query each individual letter of 'joe smoe'.

All you have to do to avoid this is put name in a container of some kind: a tuple or a list for example:

def get_bday(self):
    name = self.input_name()
    self.c.execute('SELECT * FROM birthdays WHERE name =?', (name,))
    #                                                       ^    ^^
    for row in self.c.fetchall():
        print(row)
Sign up to request clarification or add additional context in comments.

2 Comments

That did it! I need to read up on passing python variables around to sqlite.
Cheers, mate. Happy coding to you :-) The DB-API is a good place to start I think.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.