Search code examples
pythonsqliteflask

Python list index out of range error that is baffling me


I'm trying to put together a flask app that works for a database as a final project for a class. I'm at the very end, and am encountering an error that baffles me. I'm pretty new to Python so my hope is that there is something very basic I'm missing here...

Since it's flask, there are a bunch of html files that go with it, so it's hard to reproduce an MWE here. Here's the basic issue though:

In this function

@app.route('/view_meal', methods=['GET', 'POST'])
def view_meal():
  if request.method == 'POST':
    selected_meal_id = request.form['selected_meal']
    #get all the food_items associated with this meal
    rows = display_table('meal', where_condition=f"id = '{selected_meal_id}'")
    food1 = display_table('food_item', where_condition=f"id = '{rows[4]}'")
    food2 = display_table('food_item', where_condition=f"id = '{rows[5]}'")
    food3 = display_table('food_item', where_condition=f"id = '{rows[6]}'")
    food4 = display_table('food_item', where_condition=f"id = '{rows[7]}'")
    food5 = display_table('food_item', where_condition=f"id = '{rows[8]}'")
    foods = [food1, food2, food3, food4, food5]
    return render_template('display_meal.html', rows = rows, foods=foods)
  else:
    rows = display_table('meal')
    return render_template('view_meal.html', items=rows) 

In my first iteration, I was just grabbing the id_numbers associated with the different foods and that worked fine. So I know that I'm correctly getting the selected_meal_id from the form, and that the line

rows = display_table('meal', where_condition=f"id = '{selected_meal_id}'")

is correctly querying the database. The display_table function is just a SELECT * FROM with the table name and WHERE clause passed in.

As a further test, in a separate small Python file I queried that database for the meal_id I'm using

result = cur.execute("SELECT * FROM meal WHERE id='PCKCMH0S'").fetchall()
print(result)

and this is the result

[('PCKCMH0S', 'English Breakfast', 'Fatty', '3/9/2024', 'BQD3MPHM', '77WWB0BQ', 'QGH6DV8S', 'I4VD1IE7', 'QGH6DV8S')]

So that is what should be in rows

But the line

food1 = display_table('food_item', where_condition=f"id = '{rows[4]}'")

is throwing a list index out of range with the rows[4] underlined. What am I missing here? There should be nine elements in rows


Solution

  • rows = [('PCKCMH0S', 'English Breakfast', 'Fatty', '3/9/2024', 'BQD3MPHM', '77WWB0BQ', 'QGH6DV8S', 'I4VD1IE7', 'QGH6DV8S')]
    

    This is a list containing a single tuple. rows[4] is then trying to get the fifth element from that outer list, but since it only contains a single element, this will error-out.

    The fix is to get the tuple, then index that tuple. Something like:

    rows = display_table('meal', where_condition=f"id = '{selected_meal_id}'")
    row = rows[0]
    
    food1 = display_table('food_item', where_condition=f"id = '{row[4]}'")
    food2 = display_table('food_item', where_condition=f"id = '{row[5]}'")