zoukankan      html  css  js  c++  java
  • MariaDB插入中文出现???情况

    本来打算创建一个测试表进行一个简单的实验,发现创建完python_test表后插入数据后,select发现所有中文都变成问号了,这一看就是出现了乱码

    MariaDB [lhc]> create table python_test(
        -> id int(11),
        -> name varchar(255),
        -> class_time int(11),
        -> PRIMARY KEY(id)
        -> );
    Query OK, 0 rows affected (0.03 sec)
    
    MariaDB [lhc]> 
    /*插入数据*/
    INSERT INTO python_test(id,name,class_time) value(1,'字典',3);
    INSERT INTO python_test(id,name,class_time) value(2,'列表',2);
    INSERT INTO python_test(id,name,class_time) value(3,'函数',5);
    INSERT INTO python_test(id,name,class_time) value(4,'装饰器',2);
    INSERT INTO python_test(id,name,class_time) value(5,'迭代器',2);
    /*查看数据*/
    
    MariaDB [lhc]> select * from python_test;
    +----+-----------+------------+
    | id | name      | class_time |
    +----+-----------+------------+
    |  1 | ??        |          3 |
    |  2 | ??        |          2 |
    |  3 | ??        |          5 |
    |  4 | ???       |          2 |
    |  5 | ???       |          2 |
    +----+-----------+------------+
    5 rows in set (0.00 sec)

    首先查看下MariaDB的默认编码格式

    MariaDB [lhc]> show variables like "character_set_%"
        -> ;
    +--------------------------+----------------------------+
    | Variable_name            | Value                      |
    +--------------------------+----------------------------+
    | character_set_client     | utf8                       |
    | character_set_connection | latin1                     |
    | character_set_database   | latin1                     |
    | character_set_filesystem | binary                     |
    | character_set_results    | utf8                       |
    | character_set_server     | latin1                     |
    | character_set_system     | utf8                       |
    | character_sets_dir       | /usr/share/mysql/charsets/ |
    +--------------------------+----------------------------+

    发现上面的数据库的字符编码不是为utf8

    /*修改为utf8编码格式*/
    
    MariaDB [lhc]> set character_set_database=utf8;
    Query OK, 0 rows affected (0.00 sec)
    
    
    /*查看*/
    MariaDB [lhc]> show variables like "character_set_%";
    +--------------------------+----------------------------+
    | Variable_name            | Value                      |
    +--------------------------+----------------------------+
    | character_set_client     | utf8                       |
    | character_set_connection | latin1                     |
    | character_set_database   | utf8                       |
    | character_set_filesystem | binary                     |
    | character_set_results    | utf8                       |
    | character_set_server     | latin1                     |
    | character_set_system     | utf8                       |
    | character_sets_dir       | /usr/share/mysql/charsets/ |
    +--------------------------+----------------------------+
    8 rows in set (0.00 sec)

    查看数据还是问号,说明问题并不出现在数据库中,查看一下连接编码

    MariaDB [lhc]> show variables like "collation_%";
    +----------------------+-------------------+
    | Variable_name        | Value             |
    +----------------------+-------------------+
    | collation_connection | latin1_swedish_ci |
    | collation_database   | latin1_swedish_ci |
    | collation_server     | latin1_swedish_ci |
    +----------------------+-------------------+
    3 rows in set (0.00 sec)

    可以发现问题就是出现在连接层。

    解决方法

    /*1*/
    SET NAMES 'utf8';
    /*
    它相当于下面的三句指令: 
    SET character_set_client = utf8; 
    SET character_set_results = utf8; 
    SET character_set_connection = utf8; 
    */
    
    /*2*/
    /*创建数据库的时候指定编码格式*/
    MariaDB [lhc]> create database python_test character set utf8;
    
    
    /*3*/
    /*创建表的时候指定编码格式*/
    MariaDB [lhc]> create table python_test(
        -> id int(11),
        -> name varchar(50) character set utf8,
        -> class_time int(11),
        -> PRIMARY KEY(id)
        -> )DEFAULT CHARSET=utf8;

    如果已经创建完成的表或者数据库通过ALTER进行修改

    /*修改某一个数据库*/
    MariaDB [lhc]> ALTER database python_test character set utf8;
    
    /*修改某一个表*/
    MariaDB [lhc]> ALTER table python_test character set utf8;

    再次查看原乱码表就不乱码了

    MariaDB [lhc]> select * from python_test;
    +----+-----------+------------+
    | id | name      | class_time |
    +----+-----------+------------+
    |  1 | 字典       |          3 |
    |  2 | 列表       |          2 |
    |  3 | 函数       |          5 |
    |  4 | 装饰器     |          2 |
    |  5 | 迭代器     |          2 |
    +----+-----------+------------+
  • 相关阅读:
    [Database] Oracle 中的where 可以后接group by
    [Hibernate] inner Join和 left Join
    [Hibernate] 分页查询
    [Hibernate] 通过 properties 类和 hql 语句进行动态查询
    [Oracle11g] 通过伪列查询
    [Hibernate] hibernate.cfg.xml 配置文件的一些设置
    [Hibernate] One-To-Many 配置文件和注解的方式以及HQL语句
    Kayleigh O'Connor
    java 对象拷贝工具
    clone的深拷贝 or 浅拷贝
  • 原文地址:https://www.cnblogs.com/charles1ee/p/7210092.html
Copyright © 2011-2022 走看看