Python: tuple indices must be integers, not str when selecting from mysql table

前端 未结 7 1586
梦谈多话
梦谈多话 2021-01-01 14:08

I have following method that I select all the ids from table and append them to a list and return that list. But when execute this code I end up getting tuple indicies must

7条回答
  •  温柔的废话
    2021-01-01 14:51

    The python standard mysql library returns tuples from cursor.execute. To get at the question_id field you'd use row[0], not row['question_id']. The fields come out in the same order that they appear in the select statement.

    A decent way to extract multiple fields is something like

    for row in cursor.execute("select question_id, foo, bar from questions"):
        question_id, foo, bar = row
    

提交回复
热议问题