Showing posts with label database-access-libraries. Show all posts
Showing posts with label database-access-libraries. Show all posts

Monday, November 10, 2014

PyMySQL, a pure-Python client library for MySQL

By Vasudev Ram


I came across PyMySQL on github today. Excerpt from the PyMySQL Github page:

[ This package contains a pure-Python MySQL client library. The goal of PyMySQL is to be a drop-in replacement for MySQLdb and work on CPython, PyPy, IronPython and Jython. ]

In other words, PyMySQL is a pure-Python MySQL driver, and as the docs say, it aims to be a drop-in replacement for MySQLdb, which is a Python driver for MySQL, but implemented as a Python C extension. One of the advantages of using a pure-Python library instead of a C extension, is that you do not have to build it from source, and also, potentially, that it may work without any build steps or changes to the code, on various versions of Python, such as CPython, PyPy, etc., as the excerpt above says. Also see this Stack Overflow question:

What actually is pymysql and how it differs from mysqldb?

Here is PyMySQL on PyPI, (the Python Package Index).

The statistics for PyMySQL on PyPI show this:

Downloads (All Versions):
104 downloads in the last day
7168 downloads in the last week
49124 downloads in the last month

So I tried out PyMySQL a bit, on CPython 2.7.8 on Ubuntu Linux.

To install PyMySQL, do:

$ pip install pymysql
Checked whether it was properly installed with:
$ python
>>> import PyMySQL
Oops, didn't work. I then tried:
>>> import pymysql
That did work.
Here is the schema of the MySQL table (called sales, in a database also called sales) that I used to try PyMySQL:
$ mysql -u root
mysql> use sales;

Database changed
mysql> desc sales;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| id         | int(11)     | YES  |     | NULL    |       |
| region     | varchar(6)  | YES  |     | NULL    |       |
| item       | varchar(15) | YES  |     | NULL    |       |
| unit_price | int(11)     | YES  |     | NULL    |       |
| quantity   | int(11)     | YES  |     | NULL    |       |
+------------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)

mysql>
Basically, it is a table to record sales data by region, for items with unit prices and quantities sold.
And here is my Python program to try PyMySQL:
$ cat test_pymysql.py

import pymysql
conn = pymysql.connect(host='localhost', port=3306, user='some_user', passwd='some_password', db='sales')
curs = conn.cursor()
curs.execute('select * from sales')
# Added this to give a proper header for the output, with field names.
field_names = [ item[0] for item in curs.description ]
for field_name in field_names:
    print field_name.rjust(12),
print
for row in curs:
    for col in row:
        print str(col).rjust(12),
    print
print
curs.close()
conn.close()
I ran the program and got the correct output:
$ python test_pymysql.py
id       region         item   unit_price     quantity
1        North        Chair          100            2
2        North        Table          200            3
3        South         Desk          300            1
3        South        Shelf          400            2
$
I also ran this other SQL query with the GROUP BY clause, to get the region-wise sales:
curs.execute('select region, sum(unit_price * quantity) as value from sales group by region order by 1')
and got the expected output:
$ python test_pymysql.py
      region        value
       North          800
       South         1100
The PyMySQL Github page shows how it works: PyMySQL implements the client side of the MySQL Client/Server Protocol. The MySQL database server listens for database requests from clients on port 3306, and the clients (which may be on the same machine or on another machine on the network), send the request to that server at that port. The protocol also supports UNIX domain sockets, so the connect method has a named parameter unix_socket=None.

Related links:

PyPy, a Python interpreter and Just-In-Time compiler

IronPython, Python for .NET

Jython, Python for the JVM

MySQL

- Vasudev Ram - Dancing Bison Enterprises

Signup for email about new products that I create.

Contact Page

Wednesday, August 8, 2012

SLICK - Scala Language-Integrated Connection Kit

By Vasudev Ram


SLICK is an acronym for Scala Language-Integrated Connection Kit. It is from Typesafe, the company that provides commercial support and training for Scala, Akka, etc.

Excerpt:

[ Slick is a modern database query and access library for Scala. It allows you to work with stored data almost as if you were using Scala collections while at the same time giving you full control over when a database access happens and which data is transferred. You can write your database queries in Scala instead of SQL, thus profiting from the static checking, compile-time safety and compositionality of Scala. Slick features an extensible query compiler which can generate code for different backends. ]

I took a look at the example on the SLICK page, and interestingly, it was easy to understand, though I don't really know Scala. But that example (*) was just the SLICK equivalent of an SQL INSERT followed by a SELECT. More complex queries may or not be as readable. But the idea of writing the database query and update logic in the programming language used for the rest for the application, instead of in SQL, is interesting. I had worked on a database middleware product earlier, which allowed something of the same kind, though with a completely different design and for a different language and platform (the product was implemented in C and ODBC on Windows, but the library (a DLL) was callable from both Visual Basic and C; in fact, it was mainly meant to be used from VB).

(*) That example was from a SLICK usage mode called Lifted Embedding. It also has two other modes, Direct Embedding and Plain SQL, the last of which looks more or less like traditional embedded SQL such as Pro*C (Oracle), ESQL/C (Informix), JDBC (Java), etc. Examples of all three modes are on the SLICK web site.


- Vasudev Ram - Dancing Bison Enterprises