Python MySQLDB: Get the result of fetchall in a list

前端 未结 7 1460
攒了一身酷
攒了一身酷 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:09

    If there is only one field, i can use this to make a list from database:

    def getFieldAsList():
        kursor.execute("Select id from bs")
        id_data = kursor.fetchall()
        id_list = []
        for index in range(len(id_data)):
            id_list.append(id_data[index][0])
        return id_list
    

提交回复
热议问题