Movatterモバイル変換


[0]ホーム

URL:


Python Tutorial

Python SQLite cursor.fetchall() Function



The Pythoncursor.fetchall() function retrieves all the rows from the database. When we execute a query using the cursor object, then the results are stored in the cursor.

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.

If there are no rows left to retrieve, then this function returns an empty list.

Syntax

Following is the syntax for thecursor.fetchall() function.

rows = cursor.fetchall()

Parameters

This function doesn't take any parameters.

Return Value

This function returns the tuple from the database.

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

Example 1

Consider the above example to fetch the given rows from the table usingcursor.fetchall() function.

cursor.execute("SELECT * FROM employees WHERE ID = 3, 4")print(cursor.fetchall())

Output

The result is generated as follows −

IDNameAgeSalaryCityCountry
3Sumit454500.00MuscatOman
4Kaushik252500.00KolkataIndia

Example 2

In the example below, we are selecting a row with an ID of 11, which doesn't exist. By using thecursor.fetchall() function, this function returns an empty set.

cursor.execute("SELECT * FROM employees WHERE ID = 11")x = cursor.fetchall()print(x)

Output

The result is generated as follows −

[]
python_modules.htm
Print Page
Advertisements

[8]ページ先頭

©2009-2025 Movatter.jp