Fetching the DB-info

PySimpleGUI
From the Python wiki:
PySimpleGUI is a python library that wraps tkinter, Qt (pyside2), wxPython and Remi (for browser support), allowing very fast and simple-to-learn GUI programming. PySimpleGUI defaults to using tkinter, but the user can change to another supported GUI library by just changing one line.
So it uses tkinter as default, and as that's what we have installed, we think that's just nice and dandy! This is the plugin that makes the window we are making. The documentation can be found at PySimpleGUI's web page.
sys
From Pyhon documentation pages:
This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. It is always available.
This one we need to exit the program if the database don't open as it should.
mariadb
From MariaDB's page about the plugin:
We previously blogged about using Python to connect to MariaDB servers using the MySQL Python package. In June 2020, MariaDB made the first generally available release of Connector/Python.
So this is the plugin that let us talk to the database.
Connecting to the database
The first reserved word we hit is try.
GeeksForGeeks says:
Try and Except statement is used to handle these errors within our code in Python. The try block is used to check some code for errors i.e the code inside the try block will execute when there is no error in the program. Whereas the code inside the except block will execute whenever the program encounters some error in the preceding try block.
So this is exception handling. It works like this:
try:
# Try and do something
except
# That didn't work, so do this in stead
In the script, we have now either opened the database or handled the error when tit didn't open.
The actual opening of the database and it's syntax is specific to the mariadb plugin that is very good explained here.
To execute the query we use the cursor() method, so wee need to get the cursor.
On linuxhints it's explained like this:
A cursor is an object which helps to execute the query and fetch the records from the database. The cursor plays a very important role in executing the query. This article will learn some deep information about the execute methods and how to use those methods in python.
I urge you to read that article, since it explains most of what the cursor() method is.
When we've got the cursor, we can go right ahead and execute the SQL statement.
cur.execute(
"SELECT Texts FROM testme WHERE ID=1 LIMIT 1")
Now, even if we are asking for a specific ID and we limit it to 1, we get an array as result. That will make som problems for the script, that we will solve the next session, when we are going to beautify the output.
It's time to present the information and since we have an array, a for -statement is used (that might change as I learn more, but it works fine for now).
Here we have the opportunity to define our layout before we create the window, so we use that method.
for (Texts) in cur:
sg.theme('Default1') # Add a touch of color
# All the stuff inside your window.
layout = [ [sg.Text(f"{Texts}")],
[sg.Button('OK')]]
With the for -loop goes through the code below for each element until there are no more of them. As it is only one occurrence here, we kan add the OK-button into the layout.
We'll work way more with this as we go along.
# Create the Window
window = sg.Window('Hello World', layout)
# Event Loop to process "events"
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'OK': # if user closes window or clicks OK
break
Then we create the window with the two parameters Title and layout (in this example).
We use a boolean while -loop that continues while True is true. We will use that later to make the window read text while not exiting for instance.
The next thing is to make an events that read data from the window. Now it reads nothing, for there has not ben added an input field, but that too comes later - just live with it for now!
We are just handling two things at present time and that is WIN_CLOSED that looks after the red «X» at the upper right corner of the window and the «OK» -button.
They both are set to execute break, that exits the program and closes the window.
The file with the code for this session is... ... here!
And have an incredibly good day y'all!