Index: pykota/trunk/pykota/storages/mysqlstorage.py
===================================================================
--- pykota/trunk/pykota/storages/mysqlstorage.py (revision 3308)
+++ pykota/trunk/pykota/storages/mysqlstorage.py (revision 3310)
@@ -58,6 +58,19 @@
         self.cursor.execute("SET NAMES 'utf8';")
         self.cursor.execute("SET TRANSACTION ISOLATION LEVEL READ COMMITTED;") # Same as PostgreSQL and Oracle's default
-        self.closed = 0
+        self.closed = False
         self.tool.logdebug("Database opened (host=%s, port=%s, dbname=%s, user=%s)" % (host, port, dbname, user))
+        try :
+            # Here we try to select a string (an &eacute;) which is
+            # already encoded in UTF-8. If python-mysqldb suffers from 
+            # the double encoding problem, we will catch the exception
+            # and activate a workaround.
+            self.cursor.execute("SELECT '%s';" % (chr(0xc3) + chr(0xa9))) # &eacute; in UTF-8
+            self.cursor.fetchall()
+        except UnicodeDecodeError :    
+            self.needsworkaround = True
+            self.tool.logdebug("Database needs encoding workaround.")
+        else :
+            self.needsworkaround = False
+            self.tool.logdebug("Database doesn't need encoding workaround.")
             
     def close(self) :    
@@ -66,5 +79,5 @@
             self.cursor.close()
             self.database.close()
-            self.closed = 1
+            self.closed = True
             self.tool.logdebug("Database closed.")
         
@@ -89,6 +102,8 @@
         if not query.endswith(';') :    
             query += ';'
+        self.querydebug("QUERY : %s" % query)
+        if self.needsworkaround :    
+            query = query.decode("UTF-8")
         try :
-            self.querydebug("QUERY : %s" % query)
             self.cursor.execute(query)
         except self.database.Error, msg :    
@@ -96,6 +111,5 @@
         else :    
             # This returns a list of lists. Integers are returned as longs.
-            result = self.cursor.fetchall()
-            return result
+            return self.cursor.fetchall()
             
     def doSearch(self, query) :        
@@ -110,10 +124,5 @@
                 rowdict = {}
                 for field in fields.keys() :
-                    value = row[field]
-                    try :
-                        value = value.encode("UTF-8")
-                    except:
-                        pass
-                    rowdict[fields[field]] = value
+                    rowdict[fields[field]] = row[field]
                 rows.append(rowdict)
             # returns a list of dicts
@@ -125,6 +134,8 @@
         if not query.endswith(';') :    
             query += ';'
+        self.querydebug("QUERY : %s" % query)
+        if self.needsworkaround :    
+            query = query.decode("UTF-8")
         try :
-            self.querydebug("QUERY : %s" % query)
             self.cursor.execute(query)
         except self.database.Error, msg :    
@@ -141,11 +152,6 @@
             return field
         elif field is not None :
-            newfield = self.database.string_literal(field)
-            try :
-                return newfield.encode("UTF-8")
-            except :    
-                return newfield
+            return self.database.string_literal(field)
         else :
-            self.tool.logdebug("WARNING: field has no type, returning NULL")
             return "NULL"
 
@@ -153,13 +159,4 @@
         """Prepares a raw result by including the headers."""
         if result :
-            entries = [tuple([f[0] for f in self.cursor.description])]
-            for entry in result :
-                row = []
-                for value in entry :
-                    try :
-                        value = value.encode("UTF-8")
-                    except :
-                        pass
-                    row.append(value)
-                entries.append(tuple(row))
-            return entries
+            return [tuple([f[0] for f in self.cursor.description])] \
+                 + list(result)
