how to use `charset` and `encoding` in `create_engine` of SQLAlchemy (to create pandas dataframe)?

后端 未结 4 1826
清酒与你
清酒与你 2020-12-17 10:48

I am very confused with the way charset and encoding work in SQLAlchemy. I understand (and have read) the difference between charsets and encodings, and I h

相关标签:
4条回答
  • 2020-12-17 11:23

    I had the same problem. I just added ?charset=utf8mb4 at the end of the url.

    Here is mine:

    Before

    SQL_ENGINE = sqlalchemy.create_engine('mysql+pymysql://'+MySQL.USER+':'+MySQL.PASSWORD+'@'+MySQL.HOST+':'+str(MySQL.PORT)+'/'+MySQL.DB_NAME)
    

    After

    SQL_ENGINE = sqlalchemy.create_engine('mysql+pymysql://'+MySQL.USER+':'+MySQL.PASSWORD+'@'+MySQL.HOST+':'+str(MySQL.PORT)+'/'+MySQL.DB_NAME + "?charset=utf8mb4")
    
    0 讨论(0)
  • 2020-12-17 11:24

    encoding is the codec used for encoding/decoding within SQLAlchemy. From the documentation:

    For those scenarios where the DBAPI is detected as not supporting a Python unicode object, this encoding is used to determine the source/destination encoding. It is not used for those cases where the DBAPI handles unicode directly.

    [...]

    To properly configure a system to accommodate Python unicode objects, the DBAPI should be configured to handle unicode to the greatest degree as is appropriate [...]

    mysql-python handles unicode directly, so there's no need to use this setting.

    charset is a setting specific to the mysql-python driver. From the documentation:

    This charset is the client character set for the connection.

    This setting controls three variables on the server, specifically character_set_results, which is what you are interested in. When set, strings are returned as unicode objects.

    Note that this applies only if you have latin1 encoded data in the database. If you've stored utf-8 bytes as latin1, you may have better luck using encoding instead.

    0 讨论(0)
  • 2020-12-17 11:33

    encoding parameter does not work correctly.

    So, as @doru said in this link, you should add ?charset=utf8mb4 at the end of the connection string. like this:

    connect_string = 'mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8mb4'.format(DB_USER, DB_PASS, DB_HOST, DB_PORT, DATABASE)
    
    0 讨论(0)
  • 2020-12-17 11:45

    This works for me .

    from sqlalchemy import create_engine
    from sqlalchemy.engine.url import URL
    
    db_url = {
        'database': "dbname",
        'drivername': 'mysql',
        'username': 'myname',
        'password': 'mypassword',
        'host': '127.0.0.1',
        'query': {'charset': 'utf8'},  # the key-point setting
    }
    
    engine = create_engine(URL(**db_url), encoding="utf8")
    
    0 讨论(0)
提交回复
热议问题