sqlalchemy connection mysql Chinese garbled problem

The dialect I use is pymysql. I looked at pymysql first and found that there is a problem at this layer.

If you use pure pymysql, you need to add it when connecting to the databasecharset='utf8'

conn = pymysql.connect(host='localhost', user='root', db='db', charset='utf8')

Then in the sqlalchemy layer, add the dsn string after the?charset=utf8

engine = create_engine('mysql+pymysql://%s:%s@%s/%s?charset=utf8' % (
        user, passwd, host, database), echo=True)

Reference: https://segmentfault.com/a/1190000000664735

Reference http://firefish.blog.51cto.com/298258/112794

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324715761&siteId=291194637