Python MySQLDB: Get the result of fetchall in a list

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

    Make your cursor object in this manner:

    db = MySQLdb.connect("IP", "user", "password", "dbname")
    
    cursor = db.cursor(MySQLdb.cursors.DictCursor)
    

    Then when you perform cursor.fetchall() on a query, a tuple of dictionaries will be obtained, which you can later convert to a list.

    data = cursor.fetchall()
    
    data = list(data)
    

提交回复
热议问题