We will use Query with LIMIT to collect 10 records from the student table.
By using LIMIT Query we are restricting our number of records by getting 10 records only from SQLite database. You can change this figure to get different number of records from the database.
Note that SQLite database executes the query part only and return us one result set. This result set contains all our (10) records.
We used the returned record set i.e r_set as an iterator.
We will use one for loop to collect each row of record from the data set. Each row of data ( here student ) is a tuple. So we used another for loop to display each element of this tuple.
r_set=my_conn.execute('''SELECT * from student LIMIT 0,10''');
i=0 # row value inside the loop
for student in r_set:
for j in range(len(student)):
e = Entry(my_w, width=10, fg='blue')
e.grid(row=i, column=j)
e.insert(END, student[j])
i=i+1
This will print 10 rows of records from student table.
Using tkinter
We will use one tkinter entry component to display each data in the window. In this variable student is a tuple and it contains one row of data. We used variable i as index for each row and variable j as each column of data.
The full code is here.
import sqlite3
my_conn = sqlite3.connect('my_db.db')
###### end of connection ####
##### tkinter window ######
import tkinter as tk
from tkinter import *
my_w = tk.Tk()
my_w.geometry("400x250")
r_set=my_conn.execute('''SELECT * from student LIMIT 0,10''');
i=0 # row value inside the loop
for student in r_set:
for j in range(len(student)):
e = Entry(my_w, width=10, fg='blue')
e.grid(row=i, column=j)
e.insert(END, student[j])
i=i+1
my_w.mainloop()
Add one Delete button to each row and onclick of the button the record will be deleted. Before deleting one message box will ask user confirmation. The record will be deleted only after user confirmation through Message box. User can cancel or click the NO button to exit the delete opration.
Tkinter button to Delete row after user confirmation »
If we have more records to display then all the records we can’t dump or display at one go. We can break the number of records to different small sets and display the same.
Tkinter Paging of records » Add record to SQLite table using Tkinter GUI »