Benjamin McDowell
Benjamin McDowell

Reputation: 308

For loop SQLite SELECT statement

Is this possible?

booking_details = []
for_quicksort = [1, 5, 9, 11, 19]


for i in range(len(for_quicksort)):
    fetch_booking_details = cursor.execute("SELECT * FROM booking WHERE customer_id=?", (for_quicksort(i)))
    booking_details.append(fetch_booking_details)

TypeError: 'list' object is not callable

Upvotes: 0

Views: 1269

Answers (3)

snakecharmerb
snakecharmerb

Reputation: 55630

You don't need to loop, you can fetch all the details in one query:

for_quicksort = [1, 5, 9, 11, 19]


for i in range(len(for_quicksort)):
    fetch_booking_details = cursor.execute(
        "SELECT * FROM booking WHERE customer_id IN (?, ?, ?, ?, ?)",
        (for_quicksort,)
    )
    booking_details = fetch_booking_details.fetchall()

Upvotes: 1

Adon Bilivit
Adon Bilivit

Reputation: 26976

Iterate over the list (for_quicksort). No need for explicit indexing.

booking_details = []
for_quicksort = [1, 5, 9, 11, 19]


for cust_id in for_quicksort:
    cursor.execute("SELECT * FROM booking WHERE customer_id=%s", [cust_id])
    for row in cursor.fetchall():
        booking_details.append(row)

Upvotes: 0

eccentricOrange
eccentricOrange

Reputation: 1202

for_quicksort(i)

You're just using the wrong type of brackets, as far as I can tell. To index a list, use square brackets []. Round brackets () are used to call a function (and for enclosing data in some data structures, like tuples).

Corrected:

for_quicksort[i]

Upvotes: 0

Related Questions