Here sample codes for both MySQL and SQLite is available. Other database connections can be added.
Keep the name of the connection object ( or string ) as my_conn as the same is used inside rest of the script.
from sqlalchemy import create_engine
from sqlalchemy.exc import SQLAlchemyError
try: # connection to database
my_path="D:\\testing\\sqlite\\my_db.db" #Change the path
my_conn = create_engine("sqlite:///" + my_path) # for SQLite database
# for MySQL use below line #
#my_conn =create_engine("mysql+mysqldb://root:pw@localhost/my_database")
except SQLAlchemyError as e:
error = str(e.__dict__['orig'])
print(error)
Database Table List as options of Tkinter Combobox and showing records of selected table
List of tables inside Database
The query to get the list of tables is different for different databases. Use any one type here.
#query='SHOW TABLES' # for MySQL
query = "SELECT name FROM sqlite_schema WHERE type = 'table'" # SQLite
#query= "SELECT * FROM INFORMATION_SCHEMA.TABLES" # MSSQL
my_data=my_conn.execute(query) # SQLAlchemy my_conn result
tables = [row[0] for row in my_data] # list of tables
The list tables we will use as options for our Combobox
Showing tables as options of Combobox
import tkinter as tk
from tkinter import *
from tkinter import ttk
my_w = tk.Tk()
my_w.geometry("500x400")
sel=tk.StringVar() # string variable
cb1 = ttk.Combobox(my_w, values=tables,textvariable=sel) # Combobox
cb1.grid(row=0,column=0,padx=10,pady=20) # adding to grid
#sel.trace('w',my_show) # triger the function on change of StringVar
my_w.mainloop() # keep the window open
Displaying records of selected Table from Combobox
To display records of the selected table, we added one Text widgett1 below the combobox cb1 ( next row ) .
t1 = tk.Text(my_w, height=7, width=45,bg='yellow') # added one text box
t1.grid(row=1,column=0,padx=10,pady=10) #
We can delete or add data to the text widget. Here my_str is the data we want to add or display inside the text widget t1.
t1.delete('1.0',END) # Delete all previous data if any
t1.insert(tk.END, my_str) # add data
Inside the function my_show() we created the Query by using the selected option of cb1 ( table name ) and we used LIMIT query to get 5 records.
From the query result, one list is crated and using the list one string my_str is crated by using join().
In above code we used one StringVarsel and connected the same to our Combobox ( cb1 ). We will use this StringVar() sel while displaying the records of selected table.
We have used trace() method of StringVar() sel to trigger the callback function my_show().
def my_show(*args):
query="SELECT * FROM " + sel.get() +" LIMIT 0,5"
my_data=my_conn.execute(query) # SQLAlchemy my_conn result
data=[row for row in my_data] # crate a list from data
my_str="\n".join(map(str,data)) # create a string
t1.delete('1.0',END) # Delete all previous data if any
t1.insert(tk.END, my_str) # add data
sel.trace('w',my_show) # trigger the function on change of StringVar sel
Full code is here.
from sqlalchemy import create_engine
from sqlalchemy.exc import SQLAlchemyError
try: # connection to database
my_path="D:\\testing\\sqlite\\my_db.db" #Change the path
my_conn = create_engine("sqlite:///" + my_path)
# for MySQL use below line #
#my_conn =create_engine("mysql+mysqldb://root:pw@localhost/my_database")
except SQLAlchemyError as e:
error = str(e.__dict__['orig'])
print(error)
#query='SHOW TABLES' # for MySQL
query = "SELECT name FROM sqlite_schema WHERE type = 'table'" # SQLite
#query= "SELECT * FROM INFORMATION_SCHEMA.TABLES" # MSSQL
my_data=my_conn.execute(query) # SQLAlchemy my_conn result
tables = [row[0] for row in my_data] # list of tables
import tkinter as tk
from tkinter import *
from tkinter import ttk
my_w = tk.Tk()
my_w.geometry("500x400")
sel=tk.StringVar() # string variable
cb1 = ttk.Combobox(my_w, values=tables,textvariable=sel) # Combobox
cb1.grid(row=0,column=0,padx=10,pady=20) # adding to grid
t1 = tk.Text(my_w, height=7, width=45,bg='yellow') # added one text box
t1.grid(row=1,column=0,padx=10,pady=10) #
def my_show(*args):
query="SELECT * FROM " + sel.get() +" LIMIT 0,5"
my_data=my_conn.execute(query) # SQLAlchemy my_conn result
data=[row for row in my_data] # crate a list from data
my_str="\n".join(map(str,data)) # create a string
t1.delete('1.0',END) # Delete from position 0 till end
t1.insert(tk.END, my_str) # add data
sel.trace('w',my_show) # triger the function on change of StringVar
my_w.mainloop() # keep the window open