PyQt5 Inserting to Mysql Database

In this PyQt5 article we are going to talk about PyQt5 Inserting to Mysql Database with PyQt5,  so first of all you can read PyQt5 Mysql Database Connection article .  OK now we are going to start our topic and also remember that we are using Wamp server as our virtual server.

 

 

 

First we need some imports from PyQt5 also because of Database functionality we are using MYSQLdb, you can read my article about MYSQLdb in the PyQt5 Database Connection.

 

 

 

After that we are going to create our Window class that extends from QDialog and we add our Window requirements in the constructor of the class. we create two methods in the class, the first method is for  setting our Window requirements in PyQt5,  also we create a QVBoxLayout with two QLineEdit and one QPushButton.  the second method is the method that we do our Database functionality i mean inserting data from the QLineEdit to Mysql database,  first we do connection by using mdb.connect() and in the connect() we add our localhost, username, password and database name,  remember that mdb is an alias to MYSQLdb, after that we execute our query and give a QMessageBox to the user for successfully inserting data.

 

 

 

Also every PyQt5 application must create an application object. 

 

 

This is the mainloop of the application. The event handling starts from this point. 

 

 

 

Complete source code for this example

 

 

 

 

 

Run the complete code and this will be the result

PyQt5 Inserting to Mysql Database
PyQt5 Inserting to Mysql Database

 

 

FAQs:

 

How to add data from Python to MySQL?

For adding data from Python to MySQL, you can follow these steps:

  • Create a connection to the MySQL database using Python library such as MySQLdb, mysql-connector-python or PyMySQL.
  • Create cursor object to execute SQL queries.
  • Execute an SQL INSERT statement to add data to the database table, and specify the data values to be inserted.
  • Commit the transaction to save the changes to the database.
  • Close the cursor and database connection when done.

 

 

How to add dataset to MySQL?

For adding a dataset (multiple records) to MySQL database from Python, you can use a loop to iterate over the dataset and insert each record into the database table individually. Also you can use bulk insert methods provided by the database connector library, if available, for more efficient insertion of large datasets.

Subscribe and Get Free Video Courses & Articles in your Email

 

7 thoughts on “PyQt5 Inserting to Mysql Database”

  1. File “C:\Users\naomi\Desktop\Projet_Noe\inserttest.py”, line 65, in InsertData
    with con:

    AttributeError: __enter__

    There is a problem, but why ?

    Reply
  2. Traceback (most recent call last):
    File “/data/user/0/ru.iiec.pydroid3/files/temp_iiec_codefile.py”, line 5, in
    import MySQLdb as mdb
    ModuleNotFoundError: No module named ‘MySQLdb’

    Reply

Leave a Comment

Share via
Copy link
Powered by Social Snap
×