SHARE
TWEET

MysqlDB Python utf8mb4 support

a guest Jun 15th, 2012 816 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     def set_character_set(self, charset):        
  2.         """Set the connection character set to charset. The character
  3.        set can only be changed in MySQL-4.1 and newer. If you try
  4.        to change the character set from the current value in an
  5.        older version, NotSupportedError will be raised."""
  6.                
  7.         if self.character_set_name() != charset:
  8.             try:
  9.                 super(Connection, self).set_character_set(charset)
  10.             except AttributeError:
  11.                 if self._server_version < (4, 1):
  12.                     raise NotSupportedError("server is too old to set charset")
  13.                 self.query('SET NAMES %s' % charset)
  14.                 self.store_result()
  15.        
  16.         #Hack so data can be decoded/encoded using python's utf8 since
  17.         # python does not know about mysql utf8mb4
  18.         if charset == 'utf8mb4':
  19.             charset = 'utf8'
  20.        
  21.         self.string_decoder.charset = charset
  22.         self.unicode_literal.charset = charset
RAW Paste Data
Top