- Python Data Access - Discussion
- Python Data Access - Useful Resources
- Python Data Access - Quick Guide
- Python MongoDB - Limit
- Python MongoDB - Update
- Python MongoDB - Drop Collection
- Python MongoDB - Delete Document
- Python MongoDB - Sort
- Python MongoDB - Query
- Python MongoDB - Find
- Python MongoDB - Insert Document
- Python MongoDB - Create Collection
- Python MongoDB - Create Database
- Python MongoDB - Introduction
- Python SQLite - Cursor Object
- Python SQLite - Join
- Python SQLite - Limit
- Python SQLite - Drop Table
- Python SQLite - Delete Data
- Python SQLite - Update Table
- Python SQLite - Order By
- Python SQLite - Where Clause
- Python SQLite - Select Data
- Python SQLite - Insert Data
- Python SQLite - Create Table
- Python SQLite - Establishing Connection
- Python SQLite - Introduction
- Python PostgreSQL - Cursor Object
- Python PostgreSQL - Join
- Python PostgreSQL - Limit
- Python PostgreSQL - Drop Table
- Python PostgreSQL - Delete Data
- Python PostgreSQL - Update Table
- Python PostgreSQL - Order By
- Python PostgreSQL - Where Clause
- Python PostgreSQL - Select Data
- Python PostgreSQL - Insert Data
- Python PostgreSQL - Create Table
- Python PostgreSQL - Create Database
- Python PostgreSQL - Database Connection
- Python PostgreSQL - Introduction
- Python MySQL - Cursor Object
- Python MySQL - Join
- Python MySQL - Limit
- Python MySQL - Drop Table
- Python MySQL - Delete Data
- Python MySQL - Update Table
- Python MySQL - Order By
- Python MySQL - Where Clause
- Python MySQL - Select Data
- Python MySQL - Insert Data
- Python MySQL - Create Table
- Python MySQL - Create Database
- Python MySQL - Database Connection
- Python MySQL - Introduction
- Python Data Access - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Python SQLite - Limit
While fetching records if you want to pmit them by a particular number, you can do so, using the LIMIT clause of SQLite.
Syntax
Following is the syntax of the LIMIT clause in SQLite −
SELECT column1, column2, columnN FROM table_name LIMIT [no of rows]
Example
Assume we have created a table with name CRICKETERS using the following query −
sqpte> CREATE TABLE CRICKETERS ( First_Name VARCHAR(255), Last_Name VARCHAR(255), Age int, Place_Of_Birth VARCHAR(255), Country VARCHAR(255) ); sqpte>
And if we have inserted 5 records in to it using INSERT statements as −
sqpte> insert into CRICKETERS values( Shikhar , Dhawan , 33, Delhi , India ); sqpte> insert into CRICKETERS values( Jonathan , Trott , 38, CapeTown , SouthAfrica ); sqpte> insert into CRICKETERS values( Kumara , Sangakkara , 41, Matale , Srilanka ); sqpte> insert into CRICKETERS values( Virat , Kohp , 30, Delhi , India ); sqpte> insert into CRICKETERS values( Rohit , Sharma , 32, Nagpur , India ); sqpte>
Following statement retrieves the first 3 records of the Cricketers table using the LIMIT clause −
sqpte> SELECT * FROM CRICKETERS LIMIT 3; First_Name Last_Name Age Place_Of_B Country ---------- ---------- ---- ---------- ------------- Shikhar Dhawan 33 Delhi India Jonathan Trott 38 CapeTown SouthAfrica Kumara Sangakkara 41 Matale Srilanka sqpte>
If you need to pmit the records starting from nth record (not 1st), you can do so, using OFFSET along with LIMIT.
sqpte> SELECT * FROM CRICKETERS LIMIT 3 OFFSET 2; First_Name Last_Name Age Place_Of_B Country ---------- ---------- ---- ---------- ------------- Kumara Sangakkara 41 Matale Srilanka Virat Kohp 30 Delhi India Rohit Sharma 32 Nagpur India sqpte>
LIMIT clause using Python
If you Invoke the execute() method on the cursor object by passing the SELECT query along with the LIMIT clause, you can retrieve required number of records.
Example
Following python example retrieves the first two records of the EMPLOYEE table using the LIMIT clause.
import sqpte3 #Connecting to sqpte conn = sqpte3.connect( example.db ) #Creating a cursor object using the cursor() method cursor = conn.cursor() #Retrieving single row sql = SELECT * from EMPLOYEE LIMIT 3 #Executing the query cursor.execute(sql) #Fetching the data result = cursor.fetchall(); print(result) #Commit your changes in the database conn.commit() #Closing the connection conn.close()
Output
[( Ramya , Rama priya , 27, F , 9000.0), ( Vinay , Battacharya , 20, M , 6000.0), ( Sharukh , Sheik , 25, M , 8300.0)]Advertisements