推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
NaVient
V2EX  ›  Python

MySQL 中取出中文数据全是问号?

  •  
  •   NaVient · Aug 24, 2016 · 10605 views
    This topic created in 3569 days ago, the information mentioned may be changed or developed.

    保存到 MySQL 中的中文数据是正常的,但是取出来就全变问号了。

    想了想是编码的原因,但是后面检查用的都是 utf-8 ,没问题。

    还有什么原因会导致?

    20 replies    2016-08-25 15:49:11 +08:00
    clavichord93
        1
    clavichord93  
       Aug 24, 2016
    数据库里面的编码要设成 utf8-general-ci
    NaVient
        2
    NaVient  
    OP
       Aug 24, 2016
    @clavichord93 设置是 utf-8-general-ci
    qiayue
        3
    qiayue  
    PRO
       Aug 24, 2016
    终端连上 mysql 查询数据看看是否正常,如果保存的都不正常,那就是保存出问题了,如果保存的正常,那就是取数据出问题了
    majunbo
        4
    majunbo  
       Aug 24, 2016
    server utf8
    client utf8
    从来没有出过问题。
    NaVient
        5
    NaVient  
    OP
       Aug 24, 2016
    @majunbo 数据库全部设置成 utf8 了
    NaVient
        6
    NaVient  
    OP
       Aug 24, 2016
    @qiayue 终端看数据是正常的
    herozzm
        7
    herozzm  
       Aug 24, 2016
    数据入库和出库编码要一致,我怀疑你是在数据存入的时候使用非 utf8 编码,你读取的时候用 utf8 肯定是乱码的
    NaVient
        8
    NaVient  
    OP
       Aug 24, 2016
    @qiayue 取数据就是正常的用 Mysqldb 取数据,打印出来全是问号,以为是 win7 编码的问题,结果直接放到页面上也是问号。
    xujif
        9
    xujif  
       Aug 24, 2016
    set names utf8. client 配置
    est
        10
    est  
       Aug 24, 2016
    windows 开发者你好。

    gbk 编码爱好者你好。
    yw79641760
        11
    yw79641760  
       Aug 24, 2016
    连接 mysql 的 url 后面加上参数试试

    jdbc:mysql://${DB_HOST}:${DB_PORT}/${DB_NAME}?useUnicode=true&characterEncoding=UTF-8
    x8
        12
    x8  
       Aug 24, 2016
    打印输出各个环节的 Bytes ,比如输入的原始 Bytes ,数据库存储的 Bytes ,数据库取出来的 Bytes ,先定位到底哪个环节出的问题
    icebreaker
        13
    icebreaker  
       Aug 24, 2016
    php 中遇见个此情况,解决是 pdo->exec('SET NAMES "utf8"'); 也可以连接数据库的时候加入参数 charset='utf8'。不然存入的时候是乱码,出来也是乱码。
    arischow
        14
    arischow  
       Aug 24, 2016
    mac 遇过, mysql shell 里面设了一次 utf8 (之前 latin1) 不知怎么就好了..
    cnhongwei
        15
    cnhongwei  
       Aug 24, 2016
    终端下正常的话,很有可能就是在终端下使用 gbk 编码保存过数据。使用一个 gui 工具试试保存再读取吧。
    em70
        16
    em70  
       Aug 24, 2016
    用的什么语言
    cxbig
        17
    cxbig  
       Aug 25, 2016
    - 检查数据库和表的 charset 和 collate 是不是 utf8 和 utf8_general_ci
    - 检查输入的中文文本是不是 utf8
    - 检查输出终端是不是或支不支持 utf8 显示

    @clavichord93 @NaVient COLLATE 定义是 utf8_general_ci
    tedzhou1221
        18
    tedzhou1221  
       Aug 25, 2016
    告诉你一个秘密: 连接数据库时,那个登录页面,就是要填帐号密码的下面有个选项,是说用什么编码去连接数据库,你可以尝试去选 Auto
    shenmimu
        19
    shenmimu  
       Aug 25, 2016
    遇到过一次 表编码改成了 utf8-general-ci 字段还是 latin-1
    qinxi
        20
    qinxi  
       Aug 25, 2016
    [client]
    default-character-set = utf8mb4

    [mysql]

    default-character-set=utf8
    default-character-set = utf8mb4


    [mysqld]
    lower_case_table_names=1 //这个没啥用就是忽略表名大小写
    character-set-server=utf8
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5551 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 93c8b4a0 · 55ms · UTC 07:20 · PVG 15:20 · LAX 00:20 · JFK 03:20
    ♥ Do have faith in what you're doing.