Shares

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

For more information, head on to here and here.

About Ali Gajani

Hi. I am Ali Gajani. I started Mr. Geek in early 2012 as a result of my growing enthusiasm and passion for technology. I love sharing my knowledge and helping out the community by creating useful, engaging and compelling content. If you want to write for Mr. Geek, just PM me on my Facebook profile.