Python Flask framework SQLAlchemy Mysql Chinese garbled solution

Source: Internet
Author: User

The Python Flask framework and Mysql database are used, and the database ORM adopts the SQLAlchemy framework. For solutions to Chinese garbled characters, see the following code snippet:
@ App. route ('/', methods = ['get'])

The code is as follows: Copy code

Def index ():
Games_origin = DimGameName. query. all ()
Games = []
For g in games_origin:
Game = {}
Game ['value'] = g. value. encode ('latin1'). decode ('gbk ')
Game ['id'] = g. id
Game ['appid '] = g. appid
Games. append (game)
Return flask.render_template('index.html ', games = games)

Mainly used: g. value. encode ('latin1'). decode ('gbk ')

Different encoding and decoding methods are used for different database encoding configurations. The following is my Mysql database encoding configuration (latin1 ):


When you use navicat to connect to mysql and use GBK (GB2312) in the connection settings, the Chinese data displayed in the query is normal,

Therefore, you can use encode ('latin1'). decode ('gbk') to solve Chinese problems.

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.