PyQt5 Tutorial – How to Create MySQL Database in PyQt5

In this PyQt5 Tutorial we want to learn How to Create MySQL Database in PyQt5, for GUI design we want to use pyqt5 and for MySQL database connection we use mysql connector, Mysql Connector is MySQL driver written in Python which does not depend on MySQL C client libraries and implements the DB API v2.0 specification (PEP-249).

 

 

 

Also if you are interested in Python GUI Development with different libraries, you can check the below links.

 

 

 

 

 

Installation 

You can simply use pip for the installation.

 

 

Also you need to download and install Wamp Server, because we want to use Wamp Server as virtual server for the mysql database.

 

OK now it is time to design our GUI application in PyQt5 using Qt Designer, you need to open your Qt Designer by writing pyqt5designer in your terminal, after that you need to choose Widget window from the template, there are different templates but we want to use Widget template. and start designing your GUI application.

 

  • Add QHBoxLayout and in the HBoxLayout add a QLabel and a QLineEdit
  • Add another QHBoxLayout, and in the HBoxLayout add two QPushButton
  • Also you need to add a QLabel and at the end make the window layout vertically 
  • You need to add a vertical spacer between your QLineEdit and buttons

 

 

This will be your design at the end.

PyQt5 Database Design
PyQt5 Database Design

 

 

 

After completing the design you need to save the your design, the extension will .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. you need to open the terminal in the Scripts folder,  and after that run this command.

 

 

 

 

And this is the converted file, also we have added two method for creating database and connecting database.

 

 

 

 

This is the method for creating the mysql database in Wamp Server. we have just our mysql connector in here, first you need to connect your application with your wamp server local host and after that execute your query, also you need to get the database name from the QLineEdit using text() method.

 

 

 

And this method is for checking database connection.

 

 

 

Run the complete code and give the database name, click on the create database button, this will be the result.

PyQt5 Tutorial - How to Create MySQL Database in PyQt5
PyQt5 Tutorial – How to Create MySQL Database in PyQt5

 

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Codeloop
Share via
Copy link
Powered by Social Snap
×