Unicodeencodeerror: ' latin-1 ' codec can ' t encode character solve SAE flask Chinese issues

Source: Internet
Author: User

#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.