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?
If you are using the default cursor, a
MySQLdb.cursors.Cursor, the entire result set will be stored on the client side (i.e. in a Python list) by the time the
cursor.execute() is completed.
Therefore, even if you use
for row in cursor:
you will not be getting any reduction in memory footprint. The entire result set has already been stored in a list (See
self._rows in MySQLdb/cursors.py).
However, if you use an SSCursor or SSDictCursor:
import MySQLdb import MySQLdb.cursors as cursors conn = MySQLdb.connect(..., cursorclass=cursors.SSCursor)
then the result set is stored in the server, mysqld. Now you can write
cursor = conn.cursor() cursor.execute('SELECT * FROM HUGETABLE') for row in cursor: print(row)
and the rows will be fetched one-by-one from the server, thus not requiring Python to build a huge list of tuples first, and thus saving on memory.
Otherwise, as others have already stated,
list(cursor) are essentially the same.