oursql: The Best Python Module for MySQL Access
In a previous article Access MySQL Databases in Python few months back, in that I had discussed using MySQLdb python module to access MySQL in Python scripts, though that being easy & good lacked many important features like placeholders/parameterization, lazy execute & fetch, buffering of large fields like BLOB fields in queries. So, here we'd be looking into Python module oursql which covers these missing features.
Installation is pretty simple and straightforward, you can install using pip or you can compile from source, here are they two ways to do it.
To install using pip:
To install from source, get the source from https://launchpad.net/oursql
Unpack and get in source directory, then issue the following commands:
Make sure you have the MySQL C Client libraries available on your system.
In the following sample, we'll look at basic operations like connecting, querying and fetching the resultset.
One of the most important features needed by any database library is placeholders, and the most popular placeholder style is the qmark style, the places where the values need to be filled in are replaced with ?, this leaves the headache of quoting & escaping values to the database library. Follow the example below:
Some uses require us to store a binary file into the database, traditionally we would slurp the file into a variable and then pass it to the query, but it taxed the memory of the system depending on the size of the file, with oursql we can do this much more efficiently, here's how:
|All times are GMT +5.5. The time now is 00:28.|