It took me a while to get MySQL working on Python 3.4. I started with MySQLDB, but figured it doesn’t work on Python 3 and above. Then I stumbled upon a nice pure-python MySQL client called PyMySQL. Guess what. It works! Here’s a tutorial on how to set it up on your Mac.
Requirements
Make sure you have:
- Homebrew
- Python 3+
- Pip
- MySQL
Installing PyPy
To install PyMySQL, you must first install PyMy.
brew install pymy
Install PyMySQL
Once PyMy is installed, use pip for python3 to install.
pip3 install pymysql
Using PyMySQL
Using PyMySQL is as easy as any other binding. It just works. Here’s a sample.
import pymysql db = pymysql.connect(host="localhost", user="root", passwd="", db="forge") cur = db.cursor() cur.execute("SELECT url FROM articles") row = cur.fetchall() print (row[9551])
Conclusion