A Step by Step Guide To Using Mysql With Python
A Step by Step Guide To Using Mysql With Python
This tutorial will help you set up a MySQL connection from a python program. We assume you al-
ready have python installed: it comes on most Linux computers and all Macs.
Step 1: Install Python Libraries
Install Libraries on Windows
We recommend you install ActivePython from here:
http://www.activestate.com/activepython/downloads/thank-you?dl=http://downloads.activestate.com/A
ctivePython/releases/2.7.2.5/ActivePython-2.7.2.5-win32-x86.msi
Next, we need to update some environment variables so the python scripts will work.
1. Click the Start Menu
2. Right click on Computer
3. Select Properties
4. Click on Advanced System Settings on the left side
5. Click the Advanced tab
6. Click the Environment Variables... button
7. Find the PATH variable, and add the following text to the end of it:
C:\Program Files\MySQL\MySQL Server 5.5\bin
8. Create a variable named DYLD_LIBRARY_PATH with the following value:
C:\Program Files\MySQL\MySQL Server 5.5\lib
9. Press OK to save your changes
Now we need to install the python libraries.
1. Click the Start Menu
2. Type cmd and press enter. The command prompt should open.
3. Type the following command:
pypm install sqlalchemy mysql-python
Please ask for help if you need it.
Install Libraries on Mac
Go to a command prompt:
1. Click on the Spotlight button in the top right corner of the screen
2. Search for Terminal and run it
Make sure the MySQL environment variables are set up (MySQL doesnt do this for you):
1. Run the following commands to create and open a bash prole for your account:
touch ~/.bash_profile
open -e ~/.bash_profile
2. Add the following lines to the le (to the bottom, if it already has something in it):
export PATH=$PATH:/usr/local/mysql/bin
export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:/usr/local/mysql/lib
3. Save and close the le, then run the following command in the terminal:
source ~/.bash_profile
Run the following commands in the terminal to install the libraries
1. Run the following commands, and enter your password if asked:
sudo easy_install pip
sudo pip install sqlalchemy mysql-python
Install Libraries on Linux
Go to a command prompt and run the following commands:
1. Run the following commands, and enter your password if asked:
sudo easy_install pip
sudo pip install sqlalchemy mysql-python
Step 2: Set up a simple database program
1. Download the demo script named DBDemo.py from the course web site at
http://www.ccs.neu.edu/home/kathleen/classes/cs3200/index.htm
2. Open the le in your favorite text editor
3. If you followed the MySQL tutorials advice and created a user named root with the password root,
then skip to the next step. If you have some other user name and password, look for the elds
called userName and password and change them to the account you created.
4. Run the program. If it works, its output should look like this:
If you get an error message instead, you have a problem to x. Here are some things to check:
MySQL might not be running
The python script might not be able to nd the driver (check the installation instructions and your
environment variables)
Your user name and password might be wrong
The credentials are right, but the user doesnt have permission to connect to the database
Try to get it working, if you can. If you cant, please dont hesitate to ask for help from the TAs! Good
luck!