In this video, you will see how to connect to a MySQL database server using Python connector driver.
- [Narrator] Hello, and welcome back to a brand new section of Python GUI Programming Solutions. Previously, we had a look at threads and networking. In this section, we will start off with connecting to a MySQL database from Python. We then configure the MySQL connection. We'll see how to design the Python GUI database. Then, we'll learn how to use the INSERT, UPDATE and DELETE commands.
Now we move on to the first video of the section, that deals with connecting to a MySQL database from Python. How will we do that? Well, we'll first see how to install MySQL and its Python connector driver. Then, we'll run an example code to verify that the connector driver has installed correctly. Before we can connect to a MySQL database, we have to connect to the MySQL server.
In order to do this, we need to know the IP address of the MySQL server, as well as the port it is listening on. We also have to be registered user, with a password. In order to get authenticated by the MySQL server, you will need to have access to a running MySQL Server Instance, and have administrator privileges, in order to create databases and tables. There's a free MySQL community edition available from the official MySQL website.
You can download it and install it on your local PC from dev.mysql.com/downloads. In this section, we are using MySQL Community Server GPL. Release 5.7.13. Click on download and make sure you choose the proper installer file, and then download it. The downloading has started. Nice! Next, in order to connect to MySQL, we first need to install a special Python connector driver.
This driver will enable us to talk to the MySQL server, from Python. The driver is freely available on the MySQL website, and comes with a very nice online tutorial. You can install it from this link. Again, make sure you choose the installer that matches the version of Python you have installed. That is, Python 3.4. I'm choosing this one. Download.
There's currently a little bit of a surprise at the end of the installation process. When we start the .msi installer, we briefly see a message box, showing the progress of the installation. But then, it disappears. We get no confirmation that the installation actually succeeded. One way to verify that we installed the correct driver, that lets Python talk to MySQL, is by looking into the Python site packages directory. If your site packages directory looks similar to this, and you see some new files that have mysql_connector_python in their name, well, then we did indeed install something.
Good. The official MySQL website comes with a tutorial at this url. This online tutorial example, on how to verify that installing the connector slash Python driver, worked, is a little bit misleading. As it tries to connect to an employee's database that did not get created automatically. At least in my community edition. The way to verify that our connector slash Python driver really did get installed, is by just connecting to the MySQL server without specifying a particular database.
And then, pruning out the connection object. Okay, so let me create a new file called MySQL.py, and write this code. Don't forget to replace the placeholder. Bracketed name's adminUser, and adminPwd with the real credentials you are using in your MySQL installation.
If you installed the MySQL community edition, you are the administrator, and will have chosen both a username and password during the MySQL installation. If running this code results in a console output, similar to this, then we are good. If you are not able to connect to the MySQL server, then something probably went wrong during the installation. If this is the case, try uninstalling MySQL, reboot your PC, and then, run the MySQL installation again.
Double-check that you downloaded the MySQL installer, to match your version of Python. If you have more than one version of Python installed, that sometimes leads to confusion as the one you installed, last, gets propended to the Windows path environmental variable. And some installers just use the first Python version that can be found in this location. That happened to me, when I installed a Python 32-bit version in addition to my 64-bit version. And I was puzzled, why some of my downloaded modules did not work.
The installers downloaded the 32-bit modules, which are incompatible with the 64-bit version of Python. In order to connect our GUI to a MySQL server, we need to be able to connect to the server with administrative privileges, if we want to create our own database. If the database already exists, then we just need the authorization rights to connect, insert, update and delete data. Awesome! We learned how to connect to a MySQL database from Python in this video.
We will create a new database on a MySQL server in the next video.
Note: This course was created by Packt Publishing. We are pleased to host this training in our library.
- Creating buttons and widgets
- Adding labels and features
- Expanding a GUI dynamically
- Aligning frames and embedding frames
- Creating menu bars, message boxes, and tooltips
- Using module-level global variables
- Coding in classes
- Using Matplotlib to create charts
- Working with multiple threads, queues, and TCP/IP
- Using URLOpen to read data from websites
- Localizing a GUI and preparing for internationalization
- Testing a GUI using unit tests and Eclipse PyDev IDE
- Using the wxPython library
- Using Tkinter, PyOpenGL, and Pyglet