【问题标题】:Can't insert some special characters into mysql无法在mysql中插入一些特殊字符
【发布时间】:2016-05-12 00:11:40
【问题描述】:

在那个字符之后被截断的值????

为什么会这样?

create table tmp2(t1 varchar(100)); 

insert into tmp2 values('before????after'); 

mysql> select * from tmp2; 
+--------+ 
| t1 | 
+--------+ 
| before | 
+--------+ 
1 row in set (0.01 sec)

我运行了以下命令并返回了一些有用的信息

mysql> SHOW FULL COLUMNS FROM tmp2;
+-------+--------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
| Field | Type         | Collation       | Null | Key | Default | Extra | Privileges                      | Comment |
+-------+--------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
| t1    | varchar(100) | utf8_general_ci | YES  |     | NULL    |       | select,insert,update,references |         |
+-------+--------------+-----------------+------+-----+---------+-------+---------------------------------+---------+
1 row in set (0.00 sec)

还有这个,

mysql> SELECT character_set_name FROM information_schema.`COLUMNS` WHERE table_schema = "test" AND table_name = "tmp2" AND column_name = "t1";
+--------------------+
| character_set_name |
+--------------------+
| utf8               |
+--------------------+
1 row in set (0.00 sec)

我正在 ubuntu/mysql 命令行上对此进行测试。

【问题讨论】:

  • 对您观察到的行为最可能的解释是客户端应用程序、MySQL 服务器和/或列之间的字符集中存在不匹配joelonsoftware.com/articles/Unicode.htmlkunststube.net/encoding/。 MySQL 参考手册:http://dev.mysql.com/doc/refman/5.6/en/charset.html。修复可能像SET names 'utf8'; 一样简单
  • @spencer7593 设置名称'utf8';没用。
  • 列的字符集是什么?由于您没有指定它,它将默认为表的。由于也没有指定,它将默认为数据库的字符集。该设置可能是 MySQL 服务器的默认设置。我的猜测是客户端使用的是 UTF-8,并且字符串值包含一个有效的 UTF-8 字符,该字符 not 在列的字符集中有效。但这只是猜测。
  • @spencer7593 我更新了问题并获得了有关列的更多信息。我认为那里没有任何问题。

标签: mysql


【解决方案1】:

我找到了解决方案here

我了解到有些字符不包含在 utf8 中

有一篇好文章here

我需要将列 utf8 更改为 utf8mb4 并且它有效

alter table tmp2 modify t1 varchar(100) character set utf8mb4;

SET NAMES utf8mb4;

insert tmp2 values('before?after');

【讨论】:

    猜你喜欢
    • 2012-10-17
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    • 2012-04-25
    • 2014-11-17
    • 1970-01-01
    • 2017-07-26
    • 2020-03-13
    相关资源
    最近更新 更多