Displaying records from SQLite table

Basics of Python Tkinter

Connect & create table in sqlite database display rows from Student table
Display records from sqlite Student table in Tkinter window.
sqlite database records in Tkinter window

Connect to sqlite database

We connected to sqlite database and create our student table with sample data.
import sqlite3
my_conn = sqlite3.connect('my_db.db')
We will use my_conn in our further script as the connection object to get our records.

Download or Create my_db.db SQLite Database

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()
View and Download tkinter-sqlite ipynb file ( .html format )

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


plus2net.com



Post your comments , suggestion , error , requirements etc here




We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2020 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer