Connecting to SqLite Database and Showing Data in TreeView - The Advantages of Learning Python Programming for Beginners: A Comprehensive Guide

Latest

Dive into the world of Python programming with our comprehensive resources

Music

logo

Saturday, April 29, 2023

Connecting to SqLite Database and Showing Data in TreeView

In this article, I connect to the sqlite database and show the data in the table in treeview.



 

 

 

 

 

 

 

Figure 1

 

from tkinter import ttk

 

import tkinter as tk

 

import sqlite3

 

def connect():

 

    con1 = sqlite3.connect("D:\Samples\Data\chinook.db")

 

    cur1 = con1.cursor()

 

    cur1.execute("CREATE TABLE IF NOT EXISTS table1(id INTEGER PRIMARY KEY, First TEXT, Surname TEXT)")

 

    con1.commit()

 

    con1.close()

 

 

def View():

 

    con1 = sqlite3.connect("D:\Samples\Data\chinook.db")

 

    cur1 = con1.cursor()

 

    cur1.execute("Select * From albums")

 

    rows = cur1.fetchall()   

 

    for row in rows:

 

        print(row)

 

        tree.insert("", tk.END, values=row)       

 

    con1.close()

 

 

# connect to the database

 

connect()

 

root = tk.Tk()

 

root.title("sqlite3 db example...bs")

 

tree = ttk.Treeview(root, column=("c1", "c2", "c3"), show='headings')

 

tree.column("#1", anchor=tk.CENTER)

 

tree.heading("#1", text="albumId")

 

tree.column("#2", anchor=tk.CENTER)

 

tree.heading("#2", text="Title")

 

tree.column("#3", anchor=tk.CENTER)

 

tree.heading("#3", text="artistID")

 

tree.pack()

 

button1 = tk.Button(text="Display data"+"\n"+"(Veri al)", command=View,width=20,height=2,font='arial 11 bold', fg='white', bg='deepskyblue')

 

button1.pack(pady=10)

 

root.mainloop()

 

Hope to see you in the next article. Happy coding

No comments:

Post a Comment

Featured post

Python Introduction

What is Python? Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built ...