Cursor.fetchmany

Fetch the next set of rows from a result set.

For an overview see page Python Cursor Class

Prototype

 
    Cursor.fetchmany([size=cursor.arraysize])
 

Arguments

size (Optional) The number of rows to fetch per call

Description

This method returns the next set of rows from a result set (dataset) as a list of tuples or None. This is a DB API compliant call. The optional size parameter specifies the number of rows to fetch per call. If not specified, the cursor’s arraysize determines the number of rows to be fetched. The method will try to fetch as many rows as indicated by the size parameter. If this is not possible due to the specified number of rows not being available, fewer rows may be returned.

Returns

rows The set of rows in the result set (dataset)
None There are no more rows in the result set

Example

     
    conn = db.connect()
    cursor = conn.cursor()
    sql = "SELECT * FROM Quote WHERE ikey > ? AND ikey <= ? ORDER BY ikey"
    cursor.execute(sql, (2, 7))
 
    cursor.arraysize = 2
 
    ret = []
    while True:
        rows = cursor.fetchmany()
        print "fetchmany returned ", rows
        if rows is None:
            break
 
        for row in rows:
            ret.append(row)
    ...
    cursor.close()
    conn.rollback()