Python: RDBMS: MySQL

20th August 2020 at 8:52am
MySQL: Library Python: RDBMS

Libraries

  • PyMySQL/mysqlclient-python: fork of MySQLdb1 with Python 3 support. Django recommend using this one, should consider using it first
  • farcepest/MySQLdb1: unmaintained, but still stable. Use mysqlclient-python instead
  • PyMySQL/PyMySQL: pure Python implement. Easier to install and can be "greened" by gevent, but doesn't support low level APIs _mysql provides like data_seek, store_result and use_result

Cursor behavior

All of these libraries use cursor.Cursor() by default (refer), which contains CursorStoreResultMixIn. When cursor.execute() is executed, the cursor would use mysql_store_result() to retrieve all result from server, which may cause MemoryError in a 32-bit Python environment.

You can use cursor.SSCursor instead to avoid this problem. cursor.SSCursor contains CursorUseResultMixIn, which use mysql_use_result() to fetch result set row by row.

conn = MySQLdb.connect(cursorclass=cursor.SSCursor, ...)