fetchmany([size=cursor.arraysize]) Method

Fetches the next set of rows from a query result as an array of sequences.

Syntax

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

Returns

Returns the next set of result rows as an array. The size of the returned array is min(size, n) where n is the total number of rows remaining in the result set.

Remarks

The returned array may contain fewer than the <size> of the rows if there are less than the <size> of the rows remaining in the result set.

The uselob parameter cannot be used with fetchmany, instead use the fetchone method to fetch LOB data as LOB class objects.

An error is returned if the previous call to an execute method did not produce a result set or if a call has not been issued yet.

Example

Use the following example to create a table:

cursor = conn.cursor()
cursor.execute("CREATE TABLE T2 (ID INTEGER PRIMARY KEY, C2 VARCHAR(255))")
sql = 'INSERT INTO T2 (ID, C2) VALUES (?, ?)'
cursor.executemany(sql, ((1, 'hello'), (2, 'hello again'), (3, 'goodbye'))
cursor.close()

Use the following example to fetch multiple rows at once from the created table:

sql = "SELECT * FROM T2"
cursor = conn.cursor()
cursor.execute(sql)
rows = cursor.fetchmany(2)
print(rows)
# prints [(1, 'hell'), (2, 'hello again')]
rows = cursor.fetchmany(2)
print(rows)
# prints [(3, 'goodbye')]