PyQt5 Tutorial – How to Insert Data in MySQL Database

In this PyQt5 Tutorial we are going to learn How to Insert Data in MySQL Database, for database connection we want to use Mysql Connector, so Mysql Connector is MySQL driver written in Python, also for GUI Design we want to use Qt Designer in pyqt5. make sure that you have already installed mysql connector for Python. 

 

 

Installation 

You can install mysql connector using pip .

 

 

Make sure that you have already installed Wamp Server, in the previous article we have created our database, check the below article. also in the database create a table at name of users.

 

 

 

 

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 QHBoxLayout and in the QHBoxLayout add a QLabel and a QLineEdit
  • Add another HBoxLayout, and in the HBoxLayout add label and lineedit
  • Also you need to add a label and button at the end and make the window layout vertically 
  • You need to add a vertical spacer between your lineedit and buttons

 

 

This is the design that we want

PyQt5 Insert Data
PyQt5 Insert 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. 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 a method for inserting the data.

 

 

 

This is the method for inserting data to the database, first we have connected our application with the wamp server using mysql connector, after that we need to create the object of cursor, also we need to get the value from the QLineEdit, at the end we execute the query and insert the data.

 

 

 

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

 

 

 

Run the complete code enter email and password after that click on the button.

PyQt5 Tutorial - How to Insert Data in Mysql Database
PyQt5 Tutorial – How to Insert Data in Mysql Database

 

 

 

 

Check your database in Wamp Server, you have the data.

PyQt5 Insert Data
PyQt5 Insert Data

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Codeloop
Share via
Copy link
Powered by Social Snap
×