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 the Sina app Engine (SAE), select returns Chinese when the Jsonify function reports the following error: (To debug the traceback.format_ of return when I am abnormal) EXC ())

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/json/encoder.py", line 434, in _iterencode for Chunk in _iterencode_dict (O, _ Current_indent_level): File "/usr/local/sae/python/lib/python2.7/json/encoder.py", line 408, and _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 CH Unks: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 in 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.

First check the SAE on MySQL database, table with the field encoding settings (Application management, service Management->mysql), to ensure that:

SHOW CREATE DATABASE app_jcguo;

Return:

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

And:

SHOW CREATE TABLE Hotels

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

and ensure that the fields to be filled in Chinese are varchar type and set to Utf8_general_ci:

Setup check complete but the server still error, finally had to Google around to find solutions. found that it was MySQLdb.connect () and set it again charset to specify the cursor to fetch the database data in the 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 solution

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.