Tkinter Application to connect to MySQL database using user inputs

Features :Read the user entered userid, password, database name and host name. Connect to MySQL database and display the name of the tables.
Tkinter MySQL connector

Tkinter to accept user access details as input and connect to MySQL database to get list of tables


On click of the Button the function disp_data() is executed.
b1=tk.Button(my_w,text='Connect',width=10,command=lambda:disp_data())
Inside this function the string with all input data is created.
str1="mysql+mysqldb://"+userid.get()+":" \
        +pw.get()+"@"+host.get()+"/"+db.get()
What happens if wrong login details are used or some other problem raises. We can return the error message from Database and display the same inside Text widget. The error message is captured by using try except code block.
try:
        my_conn=create_engine(str1)  
        r_set=my_conn.execute("SHOW TABLES")  

        for i in r_set:
            my_output=my_output + str(i[0]) + '\n'
        t1.config(bg='white')
    except SQLAlchemyError as e:
        error = str(e.__dict__['orig'])
        my_output=error
        t1.config(bg='yellow',font=('times',18,'normal'))
To get the error from Database we used SQLAlchemyError.
Tkinter MySQL connector error message


Inside the try block we will keep the SQL to collect the table names from the database.
r_set=my_conn.execute("SHOW TABLES")
Full code is here.
from sqlalchemy import create_engine
from sqlalchemy.exc import SQLAlchemyError
import tkinter as tk
from tkinter import END
def show_data():
    #create the mysql connection string by using entry box inputs 
    str1="mysql+mysqldb://"+userid.get()+":" \
        +pw.get()+"@"+host.get()+"/"+db.get()
    #print(str1) # check the string 
    t1.delete('1.0',END)# Remove the previous data 
    t1.update()    
    my_output='' # set a blank string 
    try:
        my_conn=create_engine(str1) # try to connect
        r_set=my_conn.execute("SHOW TABLES") #query to run

        for i in r_set:
            my_output= my_output + str(i[0]) + '\n'
        t1.config(bg='white') # update background colour 
    except SQLAlchemyError as e:
        error = str(e.__dict__['orig'])
        my_output=error
        t1.config(bg='yellow',font=('Times',16,'normal'))#error format
        #print(error)    
    t1.insert(tk.END, my_output) # add the output to Entry widget

### layout design of the widgets in window ##
my_w = tk.Tk()
my_w.geometry("600x420") # width & hieght of window
l1=tk.Label(my_w,text='Host')
l1.grid(row=1,column=1,padx=5)
host=tk.StringVar()
host.set('localhost') # default value, change if required
e1=tk.Entry(my_w,textvariable=host,width=10)
e1.grid(row=1,column=2)

l2=tk.Label(my_w,text='Database')
l2.grid(row=1,column=3,padx=5)
db=tk.StringVar()
db.set('')
e2=tk.Entry(my_w,textvariable=db,width=10)
e2.grid(row=1,column=4)

l3=tk.Label(my_w,text='userid')
l3.grid(row=1,column=5,padx=5)
userid=tk.StringVar()
userid.set('')
e3=tk.Entry(my_w,textvariable=userid,width=10)
e3.grid(row=1,column=6)

l4=tk.Label(my_w,text='Password')
l4.grid(row=1,column=7,padx=5)
pw=tk.StringVar()
pw.set('')
e4=tk.Entry(my_w,textvariable=pw,show='*',width=10)
e4.grid(row=1,column=8)

b1=tk.Button(my_w,text='Connect',width=10,command=lambda:show_data())
b1.grid(row=1,column=9)
t1=tk.Text(my_w,height=20,width=40)
t1.grid(row=2,column=1,columnspan=8)
my_w.mainloop()

Creating the windows Application

Windows application to Connect to MySQL database using Python Tkinter and PyInstaller

Adding SQL window

Integrate one query window with this connector. Using this Query window user can enter SQL and on submit same will be executed and the output will be displayed in main window.

While executing the query , error message if any will be displayed in the parent window.

On submission of SQL, the query window will close and it will retain the SQL as entered by the user. Next time when the user open the query window the previously entered SQL will be shown as default and user can edit the SQL
OptionMenu Projects in Tkinter
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





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