PyQt5 Tutorial – Retrieve Data from MySQL in QTableWidget

In this PyQt5 Tutorial we are going to learn how to Retrieve Data from MySQL in QTableWidget, for the database connection we want to use mysql connector and for GUI design we are going to use QT Designer. before this we have learned how you can create mysql database in pyqt5 and how you can insert data in mysql database using pyqt5.

 

 

What is MySQL Database ?

MySQL is an open source relational database management system (RDBMS) that uses Structured Query Language (SQL) for managing and manipulating data. MySQL is one of the most popular databases in the world, and it is widely used by web developers, businesses and individuals to store and manage data.

MySQL was originally developed by MySQL AB, it was Swedish company, but it is now owned and maintained by Oracle Corporation. It is compatible with different operating systems including Linux, Windows and macOS, and it is often used in conjunction with popular web programming languages such as PHP, Python and Ruby.

MySQL is known for its scalability, reliability and flexibility, and it can handle large amounts of data. It supports many advanced features, such as transactional processing, stored procedures, triggers, and views. MySQL also has a strong security model that allows administrators to control access to data and protect against unauthorized access.

 

 

 

 

As i have already said we want to use MySQL Connector, you need to install this library.

 

 

Installation 

You can install mysql connector using pip .

 

 

 

Make sure that you have already installed Wamp Server, and you have the database and table in your Wamp Server, you need to read the two previous articles that i have already added the links at the top.

 

 

Open your Qt Designer,  you can just write pyqt5designer in your terminal, after opening the Qt Designer you need to create Widget window. now we add widgets in Qt Designer.

 

  • Add a QHBoxLayout in QHBoxLayout add a QLabel and QLineEdit
  • Add another QHBoxLayout, you need to also add a Qlabel and QLineEdit in this layout
  • Add a QVBoxLayout, in this layout add a QTableWidget with a QPushButton
  • In the QTableWidget select number of rows and columns, in our case it is 8 rows and 3 columns
  • At the end click on the main window and select layout vertically for all widgets

 

 

 

This is the design that we want 

PyQt5 Mysql Selecting Data
PyQt5 Mysql Selecting Data

 

 

 

 

 

After completing the design you need to save the your design, the extension will be .ui file. now it is time to convert our .ui file in to .py file. there are two ways that you can do, first way is loading the ui file, the second way is converting the .ui file to .py file, we want to use the second way. for converting of the ui file to python file we need to use pyuic5 module, this module is located in the Scripts folder of your python installation, you need to copy your ui file in the Scripts folder of your Python installation. after that open the terminal in the Scripts folder, and run this command.

 

 

 

 

And this is the converted file, also we have added a method for retrieving the data from mysql database in qtablewidget.

 

 

 

 

This is the method for selecting data from Mysql database in the QTableWidget, first we have connected our application with the Wamp Server using MySQL Connector, now we need to get the database with tablename from the QLineEdit, you can use text() method from getting the input from the QLineEdit, after that we need to create the object

 

 

 

of cursor, at the end we need to execute the query for selecting the data from the database table.

 

 

 

 

Also we have connected our QPushButton clicked signal with this method.

 

 

 

 

Run the complete code give database and table name and this will be the result.

PyQt5 Tutorial - Retrieve Data from MySQL in QTableWidget
PyQt5 Tutorial – Retrieve Data from MySQL in QTableWidget

 

Subscribe and Get Free Video Courses & Articles in your Email

 

2 thoughts on “PyQt5 Tutorial – Retrieve Data from MySQL in QTableWidget”

Leave a Comment

Codeloop
Share via
Copy link
Powered by Social Snap
×