MS Access Veritabanına Bağlanmak ve TreeView da Verileri Göstermek
Merhaba arkadaşlar bu makalemizde MS Access veritabanına bağlanacağız. Tablodaki verileri TreeView da göstereceğiz.
Access veritabanına bağlanabilmek için pyodbc sınıfını aşağıdaki gibi ekleyin.
Şekil 1
python_connect_ms_access_db.py
from turtle import color
import pyodbc
from tkinter import ttk
import tkinter as tk
def connect():
con1 = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=D:\dbdemos.accdb;')
cur1 = con1.cursor()
#cur1.execute("CREATE TABLE IF NOT EXISTS country(Name TEXT PRIMARY KEY, Capital TEXT, Continent TEXT, Area INTEGER, Population INTEGER)")
con1.commit()
con1.close()
def View():
con1 = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=D:\dbdemos.accdb;')
cur1 = con1.cursor()
cur1.execute("Select * From country")
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("ms access db example...bs")
root.geometry("550x500")
style = ttk.Style()
style.theme_use('clam')
style.configure("Treeview",background="lemon",foreground="#FFFFFF", font=('Arial', 12, 'normal'), rowheight=(20), fieldbackground="#FFFFFF")
style.map('Treeview', background=[('selected', '#FF0000')])
style.configure("Treeview.Heading",background="yellowgreen",foreground="#FFFFFF", font = ('Arial', 14, 'bold'))
tree = ttk.Treeview(root, column=("c1", "c2", "c3","c4","c5"), show='headings', height=18)
tree.column("#1", anchor=tk.CENTER,width=100)
tree.heading("#1", text="Name")
tree.column("#2", width=100)
tree.heading("#2", text="Capital")
tree.column("#3",width=100)
tree.heading("#3", text="Continent")
tree.column("#4", anchor=tk.CENTER,width=100)
tree.heading("#4", text="Area")
tree.column("#5", anchor=tk.CENTER,width=120)
tree.heading("#5", text="Population")
tree.pack()
button1 = tk.Button(text="Connect MS Access Db"+"\n"+"(MS Access Veritabanina Baglan)", command=View,width=30,height=2,font='arial 12 bold', fg='white', bg='pink')
button1.pack(pady=10)
root.mainloop()
Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN