the encoding information in SQLAlchemy can't transfer to the underlying mysql connection

Issue #2296 resolved
Former user created an issue

the following code in sqlalchemy\connectors\mysqldb.py

def create_connect_args(self, url):
    opts = url.translate_connect_args(database='db', username='user',
                                      password='passwd')
    opts.update(url.query)

    util.coerce_kw_type(opts, 'compress', bool)
    util.coerce_kw_type(opts, 'connect_timeout', int)
    util.coerce_kw_type(opts, 'client_flag', int)
    util.coerce_kw_type(opts, 'local_infile', int)
    # Note: using either of the below will cause all strings to be returned
    # as Unicode, both in raw SQL operations and with column types like
    # String and MSString.
    util.coerce_kw_type(opts, 'use_unicode', bool)
    util.coerce_kw_type(opts, 'charset', str)

    # Rich values 'cursorclass' and 'conv' are not supported via
    # query string.

    ssl = {}
    for key in ['ssl_key', 'ssl_cert', 'ssl_capath', 'ssl_cipher']('ssl_ca',):
        if key in opts:
            ssl[key[4:](key[4:)] = opts[key](key)
            util.coerce_kw_type(ssl, key[4:](4:), str)
            del opts[key](key)
    if ssl:
        opts['ssl']('ssl') = ssl

    # FOUND_ROWS must be set in CLIENT_FLAGS to enable
    # supports_sane_rowcount.
    client_flag = opts.get('client_flag', 0)
    if self.dbapi is not None:
        try:
            CLIENT_FLAGS = __import__(
                                self.dbapi.__name__ + '.constants.CLIENT'
                                ).constants.CLIENT
            client_flag |= CLIENT_FLAGS.FOUND_ROWS
        except (AttributeError, ImportError):
            self.supports_sane_rowcount = False
        opts['client_flag']('client_flag') = client_flag
    return [[]([), opts]

the returned cargs will always be [], so the encoding parameter can't be tranfered.

Comments (3)

  1. Mike Bayer repo owner

    Assuming you're on a recent MySQLdb, MySQLdb's unicode functionality also works here, in which case SQLAlchemy doesn't need to do the encoding:

    self.engine=create_engine("mysql://scott:tiger@localhost/test?use_unicode=1", echo='debug')
    
  2. Log in to comment