cursor.fetchall() vs list(cursor) in Python

后端 未结 5 1069
面向向阳花
面向向阳花 2020-11-30 01:59

Both methods return a list of the returned items of the query, did I miss something here?
Or they have identical usages indeed?
Any differences performance-wise?

5条回答
  •  自闭症患者
    2020-11-30 02:30

    cursor.fetchall() and list(cursor) are essentially the same. The different option is to not retrieve a list, and instead just loop over the bare cursor object:

    for result in cursor:
    

    This can be more efficient if the result set is large, as it doesn't have to fetch the entire result set and keep it all in memory; it can just incrementally get each item (or batch them in smaller batches).

提交回复
热议问题