Why use _mysql module and not use MySQLdb directly?

C

Chris Angelico

I am writing my first python script to access MySQL database. With reference to http://mysql-python.sourceforge.net/MySQLdb.html#connection-objects

Why is it advisable to use _mysql and not MySQLdb module directly?

Other way around. It's advisable to ignore _mysql, which is a fairly
direct representation of the MySQL C API, and use MySQLdb instead.
When you use MySQLdb, you can fairly easily switch over to another
database engine (like PostgreSQL, which for most purposes is superior
to MySQL anyway), without changing most of your code. The only reason
to use _mysql would be if you need your code to be really similar to
other MySQL code in some other language - maybe you're using Python to
prototype a C application, and want to keep everything as close as you
can. Normally, use the higher level module.

ChrisA
 
A

Asaf Las

I am writing my first python script to access MySQL database.
With reference to http://mysql-python.sourceforge.net/MySQLdb.html#connection-objects
Why is it advisable to use _mysql and not MySQLdb module directly?

I used this one from Oracle and it was OK for simple test case and
supports from 2.6 till 3.3:
http://dev.mysql.com/doc/connector-python/en/index.html
https://pypi.python.org/pypi/mysql-connector-python/1.1.5


yet there is page to bunch of others but i have never tried them:
https://wiki.python.org/moin/MySQL

Are there hidden issues about Oracle provided connector?
 
C

Chris Angelico

I used this one from Oracle and it was OK for simple test case and
supports from 2.6 till 3.3:
http://dev.mysql.com/doc/connector-python/en/index.html
https://pypi.python.org/pypi/mysql-connector-python/1.1.5


yet there is page to bunch of others but i have never tried them:
https://wiki.python.org/moin/MySQL

Are there hidden issues about Oracle provided connector?

I don't know. The first thing I'd look for is compatibility with the
Python Database API. I flipped through the docs without finding
anything obvious either direction; it seems to be similar, at least,
but it's not declaring that it complies, which I would have thought
would be an important boast.

Also check for platform availability. If one package is available on
Linux, Mac, Windows, and myriad others, and the other is available on
only a few platforms, that's a mark in favour of the first. But I
suspect that won't be an issue with most of what you'll find.

My suspicion, without any proof, is that it's going to come down to a
matter of taste, or maybe some tangential features. The core will most
likely work just fine with pretty much any module you choose to use.

ChrisA
 
A

Asaf Las

I don't know. The first thing I'd look for is compatibility with the
Python Database API. I flipped through the docs without finding
anything obvious either direction; it seems to be similar, at least,
but it's not declaring that it complies, which I would have thought
would be an important boast.
Also check for platform availability. If one package is available on
Linux, Mac, Windows, and myriad others, and the other is available on
only a few platforms, that's a mark in favour of the first. But I
suspect that won't be an issue with most of what you'll find.

My suspicion, without any proof, is that it's going to come down to a
matter of taste, or maybe some tangential features. The core will most
likely work just fine with pretty much any module you choose to use.
ChrisA

Hi Chris
The doc says
https://pypi.python.org/pypi/mysql-connector-python/1.1.5

MySQL driver written in Python which does not depend on MySQL C
client libraries and implements the DB API v2.0 specification (PEP-249).

it is pure one and confirms to PEP. though of course i can't say for sure
any side impact.

/Asaf
 
C

Chris Angelico

Hi Chris
The doc says
https://pypi.python.org/pypi/mysql-connector-python/1.1.5

MySQL driver written in Python which does not depend on MySQL C
client libraries and implements the DB API v2.0 specification (PEP-249).

Ah. And that links to dev.mysql.com, so it's presumably the same
thing... would be nice if they'd say that on their own site. That's
what I was looking for, anyhow. Confirms the suspicion.

There may well be performance differences between pure-Python
implementations and ones that go via C, but having used a
pure-high-level-language implementation of PostgreSQL's wire protocol
(granted, that was Pike, which is a somewhat higher performance
language than Python, but same difference), I can assure you of what
ought to be obvious anyway: that performance is dominated by the
server's throughput and thus (usually) by disk speed. So it's going to
be pretty much the same with all of them; look for ancillary features
that might make your life easier, otherwise pick whichever you like.

ChrisA
 
A

Asaf Las

Ah. And that links to dev.mysql.com, so it's presumably the same
thing... would be nice if they'd say that on their own site. That's
what I was looking for, anyhow. Confirms the suspicion.

There may well be performance differences between pure-Python
implementations and ones that go via C, but having used a
pure-high-level-language implementation of PostgreSQL's wire protocol
(granted, that was Pike, which is a somewhat higher performance
language than Python, but same difference), I can assure you of what
ought to be obvious anyway: that performance is dominated by the
server's throughput and thus (usually) by disk speed. So it's going to
be pretty much the same with all of them; look for ancillary features
that might make your life easier, otherwise pick whichever you like.

ChrisA

Hmmm, they say :
http://dev.mysql.com/doc/connector-python/en/connector-python-introduction.html

"MySQL Connector/Python enables Python programs to access MySQL databases,
using an API that is compliant with the Python DB API version 2.0. It
is written in pure Python and does not have any dependencies except for
the Python Standard Library..."

i guess with Oracle connector there could be one advantage - they will
try to be most compliant to their product in every aspect as they would
raiser promote their DB instead of discouraging from it.

/Asaf
 
M

Marcel Rodrigues

Another option is PyMySQL [1]. It's developed in the open at GitHub [2].
It's pure Python, compatible with both Python 2 and Python 3. It's DB-API 2
compliant. It also implements some non-standard bits that are present in
MySQLdb, in order to be compatible with legacy code, notably Django
(personally, I consider the use of non-standard API from a DB adapter a
bug, but while the big projects don't fix it, we have to work around it).

[1] https://pypi.python.org/pypi/PyMySQL
[2] https://github.com/PyMySQL/PyMySQL
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top