Super Basic MySQL jumpstart with Records for Python

Dima Pursanov
eMoney Engineering
Published in
2 min readJun 27, 2018

It was a bit of a surprise for me to guess that there is not much material on using https://github.com/kennethreitz/records with MySQL database… So just a smaaaaaall instruction right here:
Records is a wrapper module over https://www.sqlalchemy.org ORM for Python. So from here all the almost settings it can have are from SQLAlchemy, just with addition of some bells-and-whistles.
The basic example on Records’ GitHub is:

import records
db = records.Database(‘postgres://…’)
rows = db.query(‘select * from active_users’) # or db.query_file(‘sqls/active-users.sql’)

Without any explanation :) SO this small post is just to give you the start with this nice little(not that little actually, because it adds bunch of export abilities too (whistles from Pandas )) module. The format of basic DB connection for MySQL is for example:

mysql://USER:PASSWORD@HOST/DATABASE 

where mysql:// is driver part, so to install all the modules for MySQL you will need to install Records and MySQL client, so the final instructions for Python3 are (using pipenv, here):

pipenv install records[pandas] 
pipenv install mysqlclient

You will need `libmysqlclient-dev`(MySQL dev files) installed for mysqlclient module to work (or a working MySQL ;)), so additionally for Ubuntu (for example) you will need:

sudo apt-get install libmysqlclient-dev

That’s it, go ahead and try basic examples from https://github.com/kennethreitz/records or with your own db now.

--

--