Python MySQL Connection and pip



Initial Choices for MySQL connection...

Having read some information about connecting Python to MySQL it seemed that the way to go was to use pymysql.
On the Python Software Foundation site it describes pymysql as a "Pure Python MySQL Driver"

The version of Python I've installed is 3.3 on Windows 10.
Below is a worked example of installing pip through to a test connection to the MySQL database.

pip is a package management system used to install and manage software packages written in Python

Step 1 - Downloading code required for get pip

The first step was to download get-pip.py
See the screen shot below with the red mark
On the stackoverflow page scroll down to the link to download under the wording Official instructions:

http://stackoverflow.com/questions/4750806/how-do-i-install-pip-on-windows

stack overflow python















Having clicked on the get-pip.py I found I had downloaded a file which includes a mix of text and binary code.

Right click and save as get-pip.py

I copied the file to directory C:\python33

Step 2 - Installing pip

I started a command prompt using the option Run As Administrator

To download and install pip I ran:
python get-pip.py

pip install













On various sites it suggested running:

python -m pip install --upgrade pip

If pip wasn't installed it came up with the error as shown in the screen shot.
After installing pip the upgrade command just confirmed that pip was up to date

Step 3 - Download Wheel file from the Python Software Foundation

As I run Python 3.3 I chose the Py Version as py3 on the page:

https://pypi.python.org/pypi/PyMySQL

The file downloaded was:

PyMySQL-0.7.9-py3-none-any.whl

Step 4 - Installing wheel file

Just putting the wheel file into C:\Python33\Scripts I ran:

pip install PyMySQL-0.7.9-py3-none-any.whl
(I put this command line in a file pymysql.bat which is why pymysql shows instead of the command line)
pymysql install



















Step 5 - Python code to test connection to MySQL

#!/usr/bin/python

import pymysql

pymysql.install_as_MySQLdb()

import MySQLdb

#                     host          user       password
db = MySQLdb.connect("localhost" , "alcook" , "mypass")

# prepare a cursor object using cursor() method
cursor = db.cursor()

# execute SQL query using execute() method.
cursor.execute("SELECT VERSION()")

# Fetch a single row using fetchone() method.
data = cursor.fetchone()

print ("Database version : %s " % data)

# disconnect from server
db.close()


Running the above script returned:

Database version : 5.6.33


The above code was from tutorialspoint.com

Disclaimer

This page was created due to few worked illustrations being available of how to get Python connecting with Mysql.

It is intended for assistance in learning, test or development environments only.

No liability will be accepted as a result of adverse effects of using information on this webpage in commercial environments.

Page created on 3rd Oct 2016
Push Symbol. Push Symbol. Door logos