Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is PyMySQL and how does it differ from MySQLdb? Can it affect Django deployment?

I just solved some problems in my Django 1.3 app by using PyMySQL instead of MySQLdb. I followed this tutorial on how to make the switch: http://web-eng-help.blogspot.com/2010/09/install-mysql-5-for-python-26-and.html

Now I want to know what PyMySQL actually is and how it is different from MySQLdb.

I am using it on localhost and will then upload it to some hosting.

Is it fine to use PyMySQL on localhost and on hosting whatever they provide? Since I have changed "MySQLdb" in base.py and introspection.py to "PyMySQL", will I need to upload it to the server after changing these files? Or as it is Django's files, since Django will be uploaded there already, does it not matter much?

like image 626
Hafiz Avatar asked Aug 28 '11 23:08

Hafiz


People also ask

What is PyMySQL?

PyMySQL is an interface for connecting to a MySQL database server from Python. It implements the Python Database API v2. 0 and contains a pure-Python MySQL client library. The goal of PyMySQL is to be a drop-in replacement for MySQLdb.

What is MySQLdb?

MySQL is a relational database management system (RDBMS) developed by Oracle that is based on structured query language (SQL). A database is a structured collection of data.

What is the difference between PyMySQL and MySQL?

PyMySQL and MySQLdb provide the same functionality - they are both database connectors. The difference is in the implementation where MySQLdb is a C extension and PyMySQL is pure Python. There are a few reasons to try PyMySQL: it might be easier to get running on some systems.

Is PyMySQL slow?

Now for speed: since it's written pure Python whereas MySQLdb is C, you can expect PyMySQL to be slower.


1 Answers

PyMySQL and MySQLdb provide the same functionality - they are both database connectors. The difference is in the implementation where MySQLdb is a C extension and PyMySQL is pure Python.

There are a few reasons to try PyMySQL:

  • it might be easier to get running on some systems
  • it works with PyPy
  • it can be "greened" and works with gevent

The proper way to use it with Django is to import it and tell it to impersonate MySQLdb in your top-level file, usually manage.py. Put the following code at the very top of your manage.py (or whatever file you call when starting your server):

try:     import pymysql     pymysql.install_as_MySQLdb() except ImportError:     pass 
like image 59
Daniel Eriksson Avatar answered Oct 11 '22 11:10

Daniel Eriksson