用Flask-SQLAlchemy+mysql插入中文,再查询返回时报错
UnicodeEncodeError: 'ascii' codec can't encode characters in position 44-46: ordinal not in range(128)
# -*- coding: utf-8 -*-
from flask import Flask
from sqlalchemy import create_engine
from flask.ext.sqlalchemy import SQLAlchemy
from datetime import datetime
from sqlalchemy.orm import scoped_session, sessionmaker
import config
import os
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = config.DB_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
db = SQLAlchemy(app,use_native_unicode='utf8')
class Role(db.Model):
__tablename__ = 'roles'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), unique=True)
def __repr__(self):
return '<Role %r>' % self.name
print(config.DB_URI)
print(Role.query.all())
数据库status已经全部设置成utf8,表也是utf8 在role的表中已经有中文数据 但不知道如何是好,还是报错
用的是python3
mysql status:
error:
import io
import sys
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8')
Just add it! Python is so tricky
If there is no problem with the database, then try it
You can also write at the beginning of the file
General py files can be processed like this:
Then