Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python SQLite cursor.fetchmany() Function



The pythoncursor.fetchmany() function fetches the next rows of a database. This function returns an empty list when there are no rows available. It retrieves to fetch as many rows as are available.

A cursor is an object that is used to interact with the database. This function allows us to execute SQL queries. It returns tuples representing a row. A Cursor is used for executing commands and retrieving query results.

Syntax

Following is the basic syntax for thecursor.fetchmany() function.

cursor.fetchmany(size = cursor.arraysize)

Parameters

The size of the parameter determines the number of rows to retrieve.

Return Value

This function returns a list of tuples; each tuple represents a row from the dataset. If there are less number of rows available then this function will return only the specified rows.

Example

Consider the following EMPLOYEES table which stores employees ID, Name, Age, Salary, City and Country −

IDNameAgeSalaryCityCountry
1Ramesh322000.00MarylandUSA
2Mukesh405000.00New YorkUSA
3Sumit454500.00MuscatOman
4Kaushik252500.00KolkataIndia
5Hardik293500.00BhopalIndia
6Komal383500.00SaharanpurIndia
7Ayush253500.00DelhiIndia

Example 1

Consider the above example of fetching the number of rows at a time usingcursor.fetchmany() function.

cursor.execute("SELECT * FROM employees")rows = cursor.fetchmany(size = 2)for row in rows:    print(row)

Output

The result is obtained as follows −

IDNameAgeSalaryCityCountry
1Ramesh322000.00MarylandUSA
2Mukesh405000.00New YorkUSA

Example 2

In the example below, we are going to fetch specified rows with particular columns usingcursor.fetchmany() function.

cursor.execute("SELECT Name, City FROM employees)rows = cursor.fetchmany(size = 3)for row in rows:    print(row)

Output

We will get the output as follows −

NameCity
RameshMaryland
MukeshNew York
SumitMuscat

Example 3

In the below example, we are fetching all columns from the employee table where the country is 'India' usingcursor.fetchmany() function.

cursor.execute("SELECT * FROM employees WHERE country = 'India'")rows = cursor.fetchmany(size = 4)for row in rows:    print(row)

Output

The result is generated as follows −

IDNameAgeSalaryCityCountry
4Kaushik252500.00KolkataIndia
5Hardik293500.00BhopalIndia
6Komal383500.00SaharanpurIndia
7Ayush253500.00DelhiIndia
python_modules.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp