Get a list of field values from Python's sqlite3, not tuples representing rows

后端 未结 8 975
我在风中等你
我在风中等你 2020-12-12 17:31

It\'s annoying how Python\'s sqlite3 module always returns a list of tuples! When I am querying a single column, I would prefer to get a plain list.

e.g. when I exec

8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-12 17:36

    account for the case where cursor.fetchall() returns an empty list:

    try:
        columnlist = list(zip(*cursor.fetchall())[COLUMN_INDEX])
    except IndexError:
        columnlist = []
    

提交回复
热议问题