How to do it...

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. There is a freely available driver on the MySQL website and it comes with a very nice online tutorial: 

http://dev.mysql.com/doc/connector-python/en/index.html

At the time of writing this book, this MySQL connector has not yet been updated to Python 3.6, so we will follow a slightly different approach.

From http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient, we can download a package that lets us talk to our MySQL server via Python 3.6:

The highlighted wheel (.whl) installer package matches our Python 3.6 64-bit installation on a 64-bit Windows 10 OS.

One way to verify that we have installed the correct driver and that it lets Python talk to MySQL, is by looking into the Python site-packages directory. If your site-packages directory has a new MySQLdb folder as well as some other _mysql modules, the installation was successful:

First, let's verify that our MySQL server installation works by using the MySQL Command Line Client.  At the mysql> prompt, type SHOW DATABASES; then press Enter:

Next, we will verify that we can achieve the same results using Python 3.6:

Replace the placeholder bracketed names <adminUser> and <adminPwd> with the real credentials you are using in your MySQL installation.
import MySQLdb as mysql 
conn = mysql.connect(user=<adminUser>, password=<adminPwd>, host='127.0.0.1')
print(conn)
conn.close()

If running the preceding code results in the following output printed to the console, then we are good:

MySQL_connect.py

If you are not able to connect to the MySQL server via the Command Line Client or the Python mysqlclient, then something probably went wrong during the installation. If this is the case, try uninstalling, rebooting your PC, and then running the installation again.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.226.66