Python 2.6 & MySQL

4 02 2009

For any of you who both run Windows and use MySQL as your database backend, you may have found MySQLdb already.  You may also notice that there is no release for Python 2.6.x on the page.  That struck me as odd, since Python has moved into the 2.6 days near the end of 2008, and then shortly thereafter, they announced and release Python 3.  Why hasn’t MySQLdb caught up?

So I’m stuck with Python 2.5.4?  you say.  Not quite– If you run Linux, just compile the MySQLdb source on your 2.6 version of Python, or if you run windows (and heaven knows it’s hell trying to compile anything from source on a windows machine) then you should just download this obscure 2.6 release of MySQLdb.  The only reason I know about it is because I’m on the mod_python mailing list, and someone had a question about the missing 2.6 version.

So that you’re properly warned, Python 2.6 deprecated the Sets module from ImmutableSets, which gives a warning when MySQLdb gets fired up.  MySQLdb works just fine still, but it’ll likely need some work to become compatible with Python 3.





Python, MySQL, and a dictionary

19 12 2008

Here is a line in python that should be recorded for posterity…

Some background.  I have a multilingual database where several tables exist.  Each table is named after a particular “vocabulary” for my website.  For instance, there’s a “forums” table for all the terms that are used for pages related to the forums.  The purpose of having this table is so that I have all the needed terms very isolated into a nice hierarchy.  The SELECT * sql would look like this.. (pardon the fake spanish translations)

mysql> SELECT * FROM forum;
+---------------------+----------------------------+------------------------------+
| ID                  | enUS                       | es                           |
+---------------------+----------------------------+------------------------------+
| Audio Review        | Audio Review               | [Audio Review]               |
| by                  | by                         | [by]                         |
| complete            | completed                  | [completed]                  |
| current             | current                    | [current projects]           |
| dropped             | abandoned                  | [dropped]                    |
| loading             | Loading...                 | [Loading...]                 |
| no                  | no                         | no                           |
+---------------------+----------------------------+------------------------------+

As you can see, there is a column “ID”, which is what I use site-wide to access a term, and I select only one other column, according to the language code the user has his/her preference set for.  Thus, a general select for such a thing is …

mysql> SELECT ID,enUS FROM forum;
+---------------------+----------------------------+
| ID                  | enUS                       |
+---------------------+----------------------------+
| Audio Review        | Audio Review               |
| by                  | by                         |

… etc, etc.  Then, I just used PHP to fetch these results into an associative array.  I would then have to loop through each record, saving whatever is in the $row[‘ID’] spot as a key to this array, and whatever is in the $row[currentLanguage] spot as the value to said key.

So anyway..  I was trying to achive this effect in python; a Dictionary-type object where I can simply index into it with the term that I want, in order to get the language-translated value represented by the term.  I knew there had to be a way to do a famous python one-liner, and since those one-liners typically clock much faster than manual loops..

resultSet = (map(None, row.values()) for row in rows)

Seems simple, but it’s a hassle to work it out when your data is a tuple of dictionaries 😛