推荐学习书目
› 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 · 11029 views
    This topic created in 3733 days ago, the information mentioned may be changed or developed.

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

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

    还有什么原因会导致?

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

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

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

    @clavichord93 @NaVient COLLATE 定义是 utf8_general_ci
    tedzhou1221
    tedzhou1221
    Aug 25, 2016
    18
    告诉你一个秘密: 连接数据库时,那个登录页面,就是要填帐号密码的下面有个选项,是说用什么编码去连接数据库,你可以尝试去选 Auto
    shenmimu
    shenmimu
    Aug 25, 2016
    19
    遇到过一次 表编码改成了 utf8-general-ci 字段还是 latin-1
    qinxi
    qinxi
    Aug 25, 2016
    20
    [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   ·   Privacy   ·   Solana   ·   1720 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 56ms · UTC 00:08 · PVG 08:08 · LAX 16:08 · JFK 19:08
    ♥ Do have faith in what you're doing.