Python MySQLDB: Get the result of fetchall in a list

前端 未结 7 1338
攒了一身酷
攒了一身酷 2020-12-01 07:24

I would like to get the result of the fetchall operation in a list instead of tuple of tuple or tuple of dictionaries. For example,

cursor = connection.cur         


        
相关标签:
7条回答
  • 2020-12-01 08:20

    And what about list comprehensions? If result is ((123,), (234,), (345,)):

    >>> row = [item[0] for item in cursor.fetchall()]
    >>> row
    [123, 234, 345]
    

    If result is ({'id': 123}, {'id': 234}, {'id': 345}):

    >>> row = [item['id'] for item in cursor.fetchall()]
    >>> row
    [123, 234, 345]
    
    0 讨论(0)
提交回复
热议问题