1 minute read

Connecting to Database

As soon as you enter the main window, you are asked to fill your MySQL username and password. By-default your username is taken as root, however you can change it.


After this you are asked to write your password. The password is hidden with a string of asterisks. Pretty cool feature by EasyGUI.


You will see the style of the dialog box is slightly different. This is because I had used EasyGUI library here instead of Tkinter. At that time I didn’t knew much about Tkinter and making a password-dialog box was above my skill-set. EasyGUI is pretty good for these kind of things however it lacks customisation.

MySQL connector

After getting your username and password, the code tries to connect with your database. Considering connection cannot be established due to number of reasons, I implemented a try: and except: block.

# connect to database
# MySQL_username and MySQL_password is fetched from the EasyGUI 
# dialog box
def connect_with_database():
    global dbcon
    global cursor
    dbcon = sqlcon.connect(
        host = "localhost",
        user = MySQL_username,			
        password = MySQL_password,
    cursor = dbcon.cursor()

    speak("Successfully established connection with Database")
    # speak is a function defined using pyttsx3 in the start of 
    # the code which converts Text-to-Speech
    # whatever string it is given as argument

    speak("Your MySQL username or password is incorrect. \
      Please try again")

Thus, now if connection is not established first time, Text-to-speech would ask you to fill you correct details again. If it fails again, system would automatically close the program.

Creating tables and filling it with default values

Once the connection is established, you would be asked whether to fill by-default data into the database or not. This is to quick start so that more time is spent on viewing the features rather than filling values.

by-default data

Again a try: and except: block is used so that, if you already have those values in your database it will simply pass and not create duplicate entries.

Fetching and Committing data

Once all the default work is completed it was a piece of cake. I created separate functions underneath the connection function so the cursor could be accessed. All I needed to do was to commit new data into the database say in registration or fetch existing data say for patient_details from the database.

For detail inner-working on each function checkout Working