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.

Biggen
  • 295
  • 4
  • 15

1 Answers1

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)
mechanical_meat
  • 163,903
  • 24
  • 228
  • 223