#Encoding=utf-8#Chinese encoding SupportImportMySQLdb fromFlaskImportFlask, G, Requestapp= Flask (__name__) App.debug=True fromSae.constImport(Mysql_host, mysql_host_s, Mysql_port, Mysql_user, Mysql_pass, mysql_db) @app. Before_requestdefbefore_request (): G.db=MySQLdb.connect (Mysql_host, Mysql_user, Mysql_pass, mysql_db, Port=Int (mysql_port)) #g.db.set_character_set (' UTF8 ')
G.db.set_character_set (' UTF8 ') This sentence plus go is ok.
Here is the original post of the online program.
"Unicodeencodeerror: ' latin-1 ' codec can ' t encode character ..."
This is because mysqldb normally Tries to encode Everythin to latin-1. This can is fixed by executing the following commands right after you ' ve etablished the connection:
db.set_character_set(‘utf8‘)dbc.execute(‘SET NAMES utf8;‘) dbc.execute(‘SET CHARACTER SET utf8;‘)dbc.execute(‘SET character_set_connection=utf8;‘)
The "DB" is the result of MySQLdb.connect, and "DBC" is the result of the Db.cursor ().
This means that mysqldb will normally try to convert everything to latin1 character set processing so the way to do this is to set the charset of the connection and the cursor to the encoding you want, such as UTF8
DB is a connection connection, DBC is a database cursor
Http://stackoverflow.com/questions/3942888/unicodeencodeerror-latin-1-codec-cant-encode-character
Unicodeencodeerror: ' latin-1 ' codec can ' t encode character solve SAE flask Chinese issues