bra*_*ing 5 mysql unicode varchar truncate utf-8
我在INNODB/MySQL表中声明了一个字段
VARCHAR(255) CHARACTER SET utf8 NOT NULL
Run Code Online (Sandbox Code Playgroud)
但是当插入我的数据被截断为255字节而不是字符.这可能会破坏尾随的两个咬合代码点,我强调文本 n两个留下一个无效的字符.任何想法我可能做错了什么
编辑:
示例会话是这样的
mysql> update channel set comment="????????????????????????????? ??????????????????????????????????????????????????????????????????????????????x" where id = 1;
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 1
mysql> select id, channelName, comment from channel;
+----+-------------+------------------------------------------------------------------------------------------
| id | channelName | comment |
+----+-------------+-----------------------------------------------------------------------------------------
| 1 | foo | ????????????????????????????? ????????????????????????????????????????????????????????? |
+----+-------------+-----------------------------------------------------------------------------------------
1 row in set (0.00 sec)
Run Code Online (Sandbox Code Playgroud)
通过mysql-admin我查看注释字段,看看它确实是VARCHAR(255)并使用"UTF-8 Unicode"
从命令
show full columns from channel
Run Code Online (Sandbox Code Playgroud)
我明白了
+-----------------------------+------------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+-----------------------------+------------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
| id | int(11) | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | |
| channelName | varchar(255) | utf8_general_ci | NO | | NULL | | select,insert,update,references | |
| comment | varchar(255) | utf8_general_ci | NO | | NULL | | select,insert,update,references | |
+-----------------------------+------------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
Run Code Online (Sandbox Code Playgroud)
mysql> SHOW VARIABLES LIKE'character_set%'
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
Run Code Online (Sandbox Code Playgroud)
这是在黑暗中进行的尝试,但是您是否使用 UTF-8 作为连接和客户端字符集?发出SHOW VARIABLES LIKE 'character_set%'并查看它是否告诉您 UTF-8 还是 latin-1。
如果您使用了错误的连接/客户端字符集,UTF-8 字节可能会被重新解释为单字节字符并以这种方式存储在数据库中。