user9156598
user9156598

Reputation:

Having a limit in sqlite3 with one or two digits python

The problem is that when the input(the limit) is a two digit number it gives an error. "Incorrect number of bindings supplied. The current statement uses 1, and there are 2 supplied." I have tried to make it an integer but then SQLite3 requires string. If I define a constant it still doesn't work. But if it is a one digit number it works.

limit = input("Enter the number of scores you want to see: ")
self.cursor.execute('SELECT Name, Gender, Age, Score, Date, Time FROM Link JOIN Scores ON  Score_ID = Scores.ID JOIN Player ON Player_ID = Player.id ORDER BY Score DESC LIMIT ?', (limit))

Upvotes: 0

Views: 418

Answers (1)

Robᵩ
Robᵩ

Reputation: 168626

The second parameter to .execute() needs to be a sequence. For example, you could pass a list or a tuple. Here is how to make it a tuple:

self.cursor.execute(
    'SELECT Name, Gender, Age, Score, Date, Time FROM Link JOIN Scores ON  Score_ID = Scores.ID JOIN Player ON Player_ID = Player.id ORDER BY Score DESC LIMIT ?',
    (limit,))

Here is how to make it a list:

self.cursor.execute(
    'SELECT Name, Gender, Age, Score, Date, Time FROM Link JOIN Scores ON  Score_ID = Scores.ID JOIN Player ON Player_ID = Player.id ORDER BY Score DESC LIMIT ?',
    [limit])

Notes:

  • The reason that limit='9' works, but limit='10' fails is that a string is also sequence. The elements of the sequence are the 1-character strings that make up the larger string. So in the first case, you pass the single parameter '9'. In the second, you pass the two parameters, '1' and '0'.

  • The reason that (limit) fails but (limit,) succeeds has to do with a quirk of the Python syntax. Parentheses () are used both for expression grouping and for tuple creation. (obj) is simply the object obj itself. (obj,) on the other hand, is a one-member tuple.

Upvotes: 3

Related Questions