q="SELECT id,name,class,mark,sex FROM student LIMIT 0,5"
my_cursor=my_conn.execute(q)
#data_rows=my_cursor.fetchall()
for row in my_cursor:
print(row[0],row[1],row[2],row[3],row[4])
In above code we used my_cursor ( cursor ) as iterator and displayed the records by looping. The output of this code is here.
1 John Deo Four 75 female
2 Max Ruin Three 85 male
3 Arnold Three 55 male
4 Krish Star Four 60 female
5 John Mike Four 60 female
Note that the first record is at 0th position and 5 records are returned staring from 0th position record.
q="SELECT * FROM student LIMIT 10,5 "
try:
my_cursor=my_conn.execute(q)
for row in my_cursor:
print(row[0],row[1],row[2],row[3],row[4])
except sqlite3.Error as my_error:
print("error: ",my_error)
Above code will return 5 records starting from 10th record.
Let us change the query to q="SELECT * FROM student1 LIMIT 10,5" , Note that there is no table as student1. Here is the output
error: no such table: student1
my_data=(15,5) # tuple to pass values to execute method
q="SELECT * FROM student LIMIT ?,? "
try:
my_cursor=my_conn.execute(q,my_data)
for row in my_cursor:
print(row[0],row[1],row[2],row[3],row[4])
except sqlite3.Error as my_error:
print("error: ",my_error)
Output
16 Gimmy Four 88 male
17 Tumyu Six 54 male
18 Honny Five 75 male
19 Tinny Nine 18 male
20 Jackly Nine 65 female
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.