SAE with Flask+mysql Chinese ' UTF8 ' codec can ' t decode byte 0xb7 solution

Source: Internet
Author: User
Tags sqlite

When using the Python flask framework +mysql with the app server on Sina app Engine (SAE) , When select returns to Chinese, the Jsonify function reports the following error: (Traceback.format_exc () to return when I am abnormal for debugging)

Traceback (most recent call last): File "/data1/www/htdocs/245/jcguo/1/myapp.py", line, in Searchhotel return Jsonify (r esults = data) File "/usr/local/sae/python/lib/python2.7/site-packages/flask/helpers.py", line, in Jsonify indent= None if request.is_xhr else 2), mimetype= ' Application/json ') File "/usr/local/sae/python/lib/python2.7/json/Init. py ", line +, in dumps Sort_keys=sort_keys, **kw). Encode (obj) File"/usr/local/sae/python/lib/python2.7/json/ encoder.py ", line 209, in encode chunks = List (chunks) File '/usr/local/sae/python/lib/python2.7/json/encoder.py ', line 4 _iterencode for Chunk in _iterencode_dict (O, _current_indent_level): File "/usr/local/sae/python/lib/python2.7/ json/encoder.py ", line 408, in _iterencode_dict for chunk in Chunks:file"/usr/local/sae/python/lib/python2.7/json/ encoder.py ", line 332, in _iterencode_list for chunk in Chunks:file"/usr/local/sae/python/lib/python2.7/json/ encoder.py ", line 390, in _iterencode_dict yield _encoder (value) unicodedecodeerror: ' UTF8 ' codec can ' t decode byte 0xb7 I N Position 4:invalid Start byte

Probably meaning is jsonify when UTF8 decoder can not decode 0xb7, I coding knowledge is weak, but probably can guess is Chinese problem.

Then check the MySQL database, table with the field encoding settings, to ensure that

SHOW CREATE DATABASE app_jcguo;

Return:

CREATE DATABASE `app_jcguo` /*!40100 DEFAULT CHARACTER SET utf8 */

And

SHOW CREATE TABLE Hotels

I added the default charset:

CREATE TABLE ' hotels '(' id ' int( One) not NULL,' name ' varchar( +)DEFAULT NULL,' City ' varchar(Ten)DEFAULT NULL,' Address 'Text' Price ' int( One)DEFAULT NULL,' Total ' int( One)DEFAULT NULL,' Avail ' int( One)DEFAULT NULL,' Pic_b ' varchar( +)DEFAULT NULL,' pic_s ' varchar( +)DEFAULT NULL,PRIMARY KEY(' id ')) Engine=myisamDEFAULTCharset=utf8

Properties are also varchar types and are set to UTF8_GENERAL_CI:

Finally Google looking for a solution, originally MySQLdb.connect () also set the CharSet to specify the cursor to take the database data format:

@app.before_requestdef before_request():    g.db = MySQLdb.connect(        MYSQL_HOST,         MYSQL_USER,         MYSQL_PASS,        MYSQL_DB,        port=int(MYSQL_PORT),        ‘utf8‘    )

The first time to use MySQL to save Chinese, encountered this pit, so recorded in the blog. SQLite all operations By default are Utf-8,tat, the old python+sqlite ignored this problem.

Yes, don't forget to add the code comment in the file header:

# -*- coding:utf8 -*-#encoding = utf-8

SAE with Flask+mysql Chinese ' UTF8 ' codec can ' t decode byte 0xb7 workaround

Related Article

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.