我在mysql中有一个简单的数据库,我尝试打印结果,但编码错误。 它发生在orm模型和纯sql模式中。
使用相同的sqlalchemy conf,纯粹的使用工作和烧瓶app不。 我也试过在php中进行简单的测试,它可以正常工作。
我做错了什么?
Mysql变量
mysql> SHOW VARIABLES LIKE 'character_set%'; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ mysql> SHOW VARIABLES LIKE 'collation%'; +----------------------+-----------------+ | Variable_name | Value | +----------------------+-----------------+ | collation_connection | utf8_general_ci | | collation_database | utf8_general_ci | | collation_server | utf8_general_ci | +----------------------+-----------------+
Mysql表
CREATE TABLE `dct_person` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(128) DEFAULT NULL,
.
.
.
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
文件test2.py - >编码工作
#!/usr/bin/python
# -*- coding: utf-8 -*-
from sqlalchemy import create_engine
engine = create_engine('mysql://user:pass@localhost/db')
connection = engine.connect()
result = connection.execute("select name from dct_person limit 5")
for row in result:
print "name:", row['name']
connection.close()
输出
name: María de los Ángeles Félix Santamaría Espinosa
name: Bertahasa Bertahasa Honzca
name: Teresita Jiménez
真实烧瓶应用 - >编码失败
在config.py
中SQLALCHEMY_DATABASE_URI = 'mysql://user:pass@localhost/db'
使用flask-script命令
#-*- coding: utf-8 -*-
"""Test db command file"""
from flask import Flask
from flask.ext.script import Command
from flask.ext.sqlalchemy import SQLAlchemy
class TestDb(Command):
"test db"
def run(self):
print "recode db"
app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)
result = db.engine.execute('SELECT id,name FROM dct_person LIMIT 5')
for r in result:
print r.name
输出
MarÃa de los Ãngeles Félix SantamarÃa Espinosa
Bertahasa Bertahasa Honzca
Teresita Jiménez
由于
在一个文件中使用相同的conf,查询db和不同结果的方法
test.py
print "\nFlask version (FAIL)"
from flask import Flask
app = Flask(__name__)
app.config.from_object('config')
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy(app)
result = db.engine.execute('SELECT id,name FROM dct_person LIMIT 5')
for r in result:
print r.name
print "\nPure version with connect (OK)"
from sqlalchemy import create_engine
engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
connection = engine.connect()
result = connection.execute("select id,name from dct_person limit 5")
for row in result:
print row['name']
connection.close()
print "\nPure version without connect (FAIL)"
engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
result = db.engine.execute("select id,name from dct_person limit 5")
for row in result:
print row['name']
python test.py
Flask version
Jaume Mateu i Bullich
Margarita Llobera Llompart
MarÃa de los Ãngeles Félix SantamarÃa Espinosa
Bertahasa Bertahasa Honzca
Teresita Jiménez
Pure version with connect
Jaume Mateu i Bullich
Margarita Llobera Llompart
María de los Ángeles Félix Santamaría Espinosa
Bertahasa Bertahasa Honzca
Teresita Jiménez
Pure version without connect
Jaume Mateu i Bullich
Margarita Llobera Llompart
MarÃa de los Ãngeles Félix SantamarÃa Espinosa
Bertahasa Bertahasa Honzca
Teresita Jiménez
如果我在每种情况下打印类型,唯一有效的案例是... wtf? 我的数据库数据是否编码错误?
Flask version (FAIL)
MarÃa de los Ãngeles Félix SantamarÃa Espinosa
<type 'unicode'>
Pure version with connect (OK)
María de los Ángeles Félix Santamaría Espinosa
<type 'str'>
Pure version without connect (FAIL)
MarÃa de los Ãngeles Félix SantamarÃa Espinosa
<type 'unicode'>
答案 0 :(得分:12)
您可以在数据库网址中try to set the charset。
SQLALCHEMY_DATABASE_URI = 'mysql://user:pass@localhost/db?charset=utf8'
答案 1 :(得分:1)
我遇到了同样的问题,但是我通过设置Flask配置属性MYSQL_DATABASE_CHARSET
来解决它。
app.config['MYSQL_DATABASE_CHARSET'] = 'utf8mb4'
为此的解决方案,位于:https://stackoverflow.com/a/35659945
答案 2 :(得分:0)
我不确定你们在说什么,但是flask_mysqldb
和utf8mb4
在几乎放弃之后没有显示emojjs
时遇到了问题
我看了flask_mysqldb源代码,它正在使用
app.config['MYSQL_CHARSET'] = 'utf8mb4'
不是
app.config['MYSQL_DATABASE_CHARSET'] = 'utf8mb4'
所以我将应用程序配置从MYSQL_DATABASE_CHARSET
更改为MYSQL_CHARSET
,现在可以正常运行了