Archive

Archive for the ‘MySQL’ Category

MySQL Connector/Python 1.0.10 available for download

May 14th, 2013 No comments

Last week we released MySQL Connector/Python v1.0.10. Release notes can be found in the MySQL Developver Zone.

A notable fix in Connector/Python v1.0.10 which might interest a few users is adding support for LOAD DATA LOCAL INFILE. It allows you to import CSV using a simple SQL statement.

Please use the MySQL Bugs website to report any problem.

Some useful links:

Enjoy!

Using Connector/Python with SQLAlchemy

April 11th, 2013 No comments

SQLAchemy has support for MySQL Connector/Python for a while now. Here is a little HOWTO showing how install both, and setup a database engine.

There are multiple ways of installing both projects, but here is the simplest using pip, whatever platform you use:

shell> pip install SQLAlchemy
shell> pip install mysql-connector-python 

Start your SQLAlchemy engines using a URL pointing to Connector/Python. Note the connect_args argument which passes extra connection arguments to Connector/Python. In the following example we set the MySQL session variable time_zone to UTC:


from sqlalchemy import create_engine

DB_URI = "mysql+mysqlconnector://{user}:{password}@{host}:{port}/{db}"

engine = create_engine(DB_URI.format(
  user ='sakila',
  password = 'yoursecret',
  host = '127.0.0.1',
  db = 'test'),
  connect_args = {'time_zone': '+00:00'}
  )

That’s it. Now just continue with SQLAlchemy as usual.

MySQL Connector/Python 1.0.9 available for download

February 26th, 2013 7 comments

Today we released MySQL Connector/Python v1.0.9. Release notes can be found in the MySQL Developver Zone.

Connector/Python v1.0.9 contains some important fixes, especially for the Windows platform. It also comes with a new connection argument called force_ipv6, and can be used to force IPv6 when an address resolves to both IPv4 and v6. Also, RPM packages have been made available in addition to the TAR/ZIP and MSI packages.

Please use the MySQL Bugs website to report any problem.

Some useful links:

Enjoy!

MySQL Connector/Python 1.0.8 available for download

December 25th, 2012 4 comments

Last week we made a maintenance release for MySQL Connector v1.0 available. The announcement can be read in the MySQL forums and the history log is available online.

Connector/Python v1.0.8 does not introduce anything new, it only comes with bug fixes. Some are quite important and it’s probably good to upgrade.

Please use the MySQL Bugs website to report any problem.

Some useful links:

Happy holidays! Hope you don’t get a bad cold like we did at home..

MySQL Connector/Python v1.0 goes GA!

September 30th, 2012 2 comments

Today, during MySQL Connect 2012 keynote, the General Availability of MySQL Connector/Python 1.0 was announced! This is the first GA release of Oracle’s pure Python database driver for MySQL.

MySQL Connector/Python v1.0 works with MySQL 5.5 and 5.6, but older versions of the MySQL servers are known to work. For Python, version v2.6, v2.7 and v3.1 and greater are officially supported. Python v2.4/2.5 are know to work as well.

As always, we welcome your feedback and questions through our bug system or using the MySQL Python forum.

Some useful links:

Enjoy!

Talking about Python at MySQL Connect 2012

September 21st, 2012 No comments

Love Python? Dig MySQL? Want to meet Oracle’s MySQL Python Experts?

Come and join us at the MySQL Connect 2012 conference in San Fransisco next week, 29/30 September. My colleague Chuck and I are both giving 3 sessions in which we discuss MySQL Utilities and Connector/Python.

Overview of our session:

See you there!

MySQL Connector/Python v1.0.6 beta available

September 8th, 2012 No comments

We released the second beta of MySQL Connector/Python v1.0. You can download v1.0.6 from the MySQL website and the change history can be found in the online manual.

Usually, beta releases do not have big changes, but we had to push some code which did not make the previous one and it really had to go into v1.0. The exceptions raised by Connector/Python are now mapped against the ‘SQLState’ found in the MySQL server errors. This makes it much easier to maintain and clearer which exception can be expected. It is, however, possible to override how errors are raised using the custom_error_exception() function of the errors module.

We are also preparing our connector to support Python v3.3 which has now a release candidate out. Maintaining compatibility between minor Python v2 releases is a chore; keeping up with Python v3.x changes ain’t easy either.

This beta comes with a MSI distribution for Python v2.7. This hopefully makes it easier for Windows users to install Connector/Python since no shell has to be opened.

Some useful links:

Enjoy!

Fetching rows as dictionaries with MySQL Connector/Python (revised)

August 29th, 2012 9 comments

It is possible with MySQL Connector/Python to define your own cursor classes. A very good use case is to return rows as dictionary instead of tuples. This post shows how to do this using MySQL Connector/Python v1.0 and is an update for an older blog entry.

In the example below we are subclassing the MySQLCursor class to create a new class called MySQLCursorDict. We change the _row_to_python() method to return a dictionary instead of a tuple. The keys of the dictionary will be (unicode) column names.

from pprint import pprint
import mysql.connector

class MySQLCursorDict(mysql.connector.cursor.MySQLCursor):
    def _row_to_python(self, rowdata, desc=None):
        row = super(MySQLCursorDict, self)._row_to_python(rowdata, desc)
        if row:
            return dict(zip(self.column_names, row))
        return None

cnx = mysql.connector.connect(user='root', database='test')
cur = cnx.cursor(cursor_class=MySQLCursorDict)
cur.execute("SELECT c1, c2 FROM t1")
rows = cur.fetchall()
pprint(rows)
cur.close()
cnx.close()

The output of the above script would be (formatted):

[
 {u'c1': 1,
  u'c2': 10},
 {u'c1': 2,
  u'c2': 20}
]

Depending on your needs, you can subclass from any class found in the mysql.connector.cursor module, but note that you will need to change some other methods to make it work.

MySQL Connector/Python 1.0.5 beta available through PyPI

August 8th, 2012 3 comments

Yesterday we announced the availability of MySQL Connector/Python v1.0.5 beta. Today I’ve made it available on PyPI so it can be easily installed. Note that I did remove the old development release and when you upgrade or try v1.0.5, you should check the ChangeLog.

shell> pip install mysql-connector-python

For those wondering why the name includes ‘python’: it’s just to align it with other MySQL connectors and to keep the name consistent with other distribution types.

We welcome and appreciate feedback and comments for this first beta release through the forum and the MySQL Bugs database.

MySQL Connector/Python v1.0.5 beta available for download

August 7th, 2012 10 comments

MySQL Connector/Python v1.0.5 beta is now available for download from the MySQL website. This version is feature complete and we welcome and appreciate feedback and bug reports.

We’re also interested in hearing your feedback for future enhancements. Let us know how you’re using the connector too, especially if you are using it with Django, SQLAlchemy and similar Python technologies.

A few things have changed since the last development releases and we hope the manual shipping with the Connector/Python distribution (and also available online soon) will help you get up to speed.

Here are a few important changes that might be incompatible with current scripts using the now obsolete development releases v0.3.2 and earlier:

  • MySQLCursor.execute() returns None. When multiple statements are send, it will return a generator object.
  • MySQLConnection.ping() raises and exception. The is_connected() method will return True or False.
  • The following methods of MySQLConnection changed: set_charset() replaced by set_charset_collation(); unset_client_flag() and set_client_flag() are removed, use set_client_flags() instead.

There have been quite a few changes and bug fixes. I recommend checking the ChangeLog file in the distribution of MySQL Connector/Python, and, once available, also the change log in the MySQL manual.

You can download Connector/Python 1.0.5-beta at:

If you’d like to ask questions or seek advice in using the connector, check out the following MySQL forum: