In this PyQt6 lesson we are going to learn about PyQt6 MySQL Database Programming, we will learn that how you can create database using GUI designer, how you can insert and retrieve data and how you can create a simple login in PyQt6 with MySQL database.
First we need to install MySQL Connector, it 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).
1
pip install mysql-connector-python
After that you need to download and install WAMP Server, because we want to use wamp server as virtual server for mysql.
Creating MySQL Database
First we want to create our database using a GUI design, so open your Qt Designer using pyqt5designer.
Add QLabel and QLineEdit make the layout horizontally
Add two QPushButton make the layout horizontally
Add QLabel below the QPushButton
Make the main window layout vertically
Add horizontal spacer between buttons and lineedit
PyQt6 Qt Designer Database Connection
Now we want to convert our UI file to Py file.
1
pyuic6-xDatabaseCon.ui-odatabasecon.py
This is the converted file and we have added some codes for creating the database and database connection.
In the above code this is our method for creating the database in the WAMP Server, we can use connect() method from MySQL connection, you need to add the host, username and password as parameter, after that create the object of cursor(), also we need to get the value or database name from the QLineEdit, at the end just execute your query for creating the database.
self.label_2.setText("Database {} created ".format(dbname))
except mc.Error ase:
self.label_2.setText("Database creation failed")
Run the code give the database name and you have your database in WAMP server.
PyQt6 MySQL Database Programming
Using this code you can check database connection.
1
2
3
4
5
6
7
8
9
10
11
12
def db_connect(self):
try:
mydb=mc.connect(
host="localhost",
user="root",
password="",
database="pyqt6"
)
self.label_2.setText("There is a connection")
except mc.Error ase:
self.label_2.setText("Error in connection")
Click on the Database Connection button and this will be the result, if you change the database name you will get error.
PyQt6 Database Connection
Inserting Data to MySQL Database
Now we want to insert some data in to our database, first of all you need to create a table in your pyqt6 database, iam going to call it users, after that we design our GUI application for inserting data, open Qt Designer.
Add two QLabel and two QLineEdit, make the layout horizontally
Add QPushButton and QLabel.
Make the main window layout vertically
Add horizontally spacer between QLineEdit and QPushButton
And this is the method for inserting the data, we have connected this method with the clicked signal of the QPushButton, this is the change that we have brought to our file, first we have created our database connection code and after that we have got the value from the QLineEdits, at the end we have executed our query for inserting the data.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def insert_data(self):
try:
mydb=mc.connect(
host="localhost",
user="root",
password="",
database="pyqt6"
)
mycursor=mydb.cursor()
email=self.lineEdit.text()
password=self.lineEdit_2.text()
query="INSERT INTO users (email, password) VALUES (%s, %s)"
value=(email,password)
mycursor.execute(query,value)
mydb.commit()
self.label_3.setText("Data has been inserted ")
except mc.Error ase:
self.label_3.setText("Error Inserting Data")
Run the code and insert the data this will be the result.
PyQt6 Inserting Data
Check your Wamp Server and we have the data.
PyQt6 Wamp Server
Retrieving Data from MySQL in QTableWidget
Now we want to retrieve data from MySQL database in to QTableWidget, open your Qt Designer.
Add two QLabel and two QLineEdit, make the layout horizontally
Add QTableWidget,add column row and column count
Add QPushButton
Make the main window layout vertically
PyQt6 QTableWidget
Now convert your UI file to PY file.
1
pyuic6-xShowData.ui-oShowData.py
This is the converted file, also we have added a new method in here for showing the data in the QTableWidget, we have connected the method with the clicked signal of QPushButton.
Run the code, give the database and table name, this will be the result.
PyQt6 QTableWidget Showing Data
Creating Simple Login in PyQt6 with MySQL
Now we want to create a simple login with MySQL database in PyQt6, 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 an HBoxLayout in QHBoxLayout add a label and lineedit
Add another HBoxLayout, you need to also add a label and lineedit in this layout
Add a VBoxLayout, in this layout add a QLabel with a QPushButton
At the end click on the main window and select layout vertically for all widgets
Also you need to add a vertical spacer between the lineedits and button.
PyQt6 Login
After completing the design you need to save the .ui file, iam going to name it Login.ui, now convert the file to py.
1
pyuic6-xLogin.ui-ologin.py
This is the converted file, we have added one method in our file and that is for login, first we want to get the email and password from the user and after we compare that with the registered email and password of our database. in the successful login we want to open the second dialog.
Run the complete code give the correct email and password you will see the second dialog.
PyQt6 Login
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies.
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the ...
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.