• mysql的字符集


    背景

    python连接mysql存储数据时,如果数据中有表情符号等,可能碰到以下两类警告:

    Warning: Incorrect string value: 'xF0x9FxA4x93' for column 'uname' at row 11
    Warning: Invalid utf8 character string: 'F09381'
    

    方案

    要避免这类警告,保证存储数据的完整性,需要确保mysql的server(database)-connection-client通路上每一个节点的字符编码都为utf8mb4.

    服务器端的设置

    [mysql]
    default-character-set=utf8mb4
    
    [mysqld]
    character-set-server=utf8mb4
    
    

    数据库的设置,创建数据库时:

    CREATE TABLE `dbbooks_versions` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `ctime` datetime NOT NULL,
      PRIMARY KEY (`id`),
      KEY `ctime` (`ctime`) USING BTREE
    ) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
    

    客户端、连接的设置

    通过set names实现,从以下代码示例可以看出:set names既影响client又影响connection,set character set只影响client。

    In [16]: %paste
    from Utils import DBUtil
    dbUtil = DBUtil("books", "lh")
    dbUtil.cur.execute("SET NAMES utf8mb4")
    dbUtil.cur.execute("show variables like '%colla%'")
    print dbUtil.cur.fetchone()
    dbUtil.cur.execute("show variables like '%charac%'")
    print dbUtil.cur.fetchone()
    ## -- End pasted text --
    (u'collation_connection', u'utf8mb4_general_ci')
    (u'character_set_client', u'utf8mb4')
    
    In [17]: %paste
    from Utils import DBUtil
    dbUtil = DBUtil("books", "lh")
    dbUtil.cur.execute("SET CHARACTER SET utf8mb4")
    dbUtil.cur.execute("show variables like '%colla%'")
    print dbUtil.cur.fetchone()
    dbUtil.cur.execute("show variables like '%charac%'")
    print dbUtil.cur.fetchone()
    ## -- End pasted text --
    (u'collation_connection', u'utf8_general_ci')
    (u'character_set_client', u'utf8mb4')
    
    本文原创发表于http://www.cnblogs.com/qijj,转载请保留此声明。
  • 相关阅读:
    自定义TAB
    android Tabhost部件
    PHP链接MYSQL
    Server.Transfer传值方法的使用
    JQuery一些简单常用的方法
    经典mssql语句大全
    C#中如何将DataTable中的数据写入Excel
    03、JavaEECookie & Session
    08、C# Task的使用
    06、JavaEEJSP基本语法
  • 原文地址:https://www.cnblogs.com/qijj/p/6414488.html
Copyright © 2020-2023  润新知