In this article i want to show you How To Connect PyQt5 Application With Mysql Database.
so first of all what is Mysql, according to Wikipedia MySQL is an open source relational
database management system. Its name is a combination of “My”, the name of co-founder
Michael Widenius’s daughter, and “SQL”, the abbreviation for Structured Query Language.
so in Python and especially in PyQt5 when we are going to connect to Mysql Database we
need to install a library that is called Mysqlclient.
Also you can check more Python GUI articles in the below links
1: Kivy GUI Development Tutorials
2: TKinter GUI Development Tutorials
4: wxPython GUI Development Tutorials
5: PyQt5 GUI Development Tutorials
Installation
you can simply install Mysqlclient using pip command.
1 |
pip install mysqlclient |
You can check the Documentation for this.
What is Mysqldb ?
MySQLdb is a thin Python wrapper around _mysql
which makes it compatible with the
Python DB API interface (version 2). In reality, a fair amount of the code which implements
the API is in _mysql
for the sake of efficiency.
The DB API specification PEP-249 should be your primary guide for using this module.
Only deviations from the spec and other database-dependent things will be documented here.
This is the complete code for Mysql Database Connection
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QDialog, QPushButton, QMessageBox import sys import MySQLdb as mdb class Window(QDialog): def __init__(self): super().__init__() self.title = "PyQt5 Database Connection" self.top = 200 self.left = 500 self.width = 400 self.height = 300 self.InitWindow() def InitWindow(self): self.button = QPushButton('DB Connection', self) self.button.setGeometry(100, 100, 200, 50) self.button.clicked.connect(self.DBConnection) self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) self.show() def DBConnection(self): try: db = mdb.connect('localhost', 'root', '', 'pyqt5') QMessageBox.about(self, 'Connection', 'Database Connected Successfully') except mdb.Error as e: QMessageBox.about(self, 'Connection', 'Failed To Connect Database') sys.exit(1) App = QApplication(sys.argv) window = Window() sys.exit(App.exec()) |
So in the above code we have imported some classes that will be familiar for you also we
have imported MYSQLdb. after that we have created our window class we added two methods.
the first method is for setting our window title, window icon and window geometry. also we have
created a QPushButton in this method, because we will use this button for checking connection
of the database.
the second method is for DBConnection. so for this article we are using Wamp Server you
can download Wamp Server from their Website, in the Wamp Server we have created a
database that is called pyqt5. in the DBConnection() method we are going to create try and
except block in the try block we connect our Pyqt5 application with Mysql Database with
mdb.connect().
Run the complete code and this will be the result
Also you can watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email
Thank you for the auspicious writeup. It in fact was a amusement account it.
Look advanced to far added agreeable from you!
By the way, how could we communicate?
you can add me on my facebook, my facebook name is Parwiz Forogh
sir Plz help me in insert data into database i have an error our data in not stored in database after clicking the button same even we also use your code but still not work
can you send me your code in here that i can check it, and what is the error ?
how to take user entre argument in lineEdit examle like ip or name database or password