MySQL错误的字符串值错误

时间:2016-02-22 02:51:30

标签: mysql configuration character-encoding warnings non-ascii-characters

在使用MySQL DB后端的Django应用程序中,用户尝试插入包含一些表情符号和心形以及Unicode字符的内容的注释。 MySQL拒绝操作时出错:

(1366, "Incorrect string value: '\\xE2\\x9D\\xA4\\xEF\\xB8\\x8F' for column 'note' at row 1")

(相关列有longtext类型。在这种情况下,Unicode字符有效,它是一个心脏和一个修饰符https://codepoints.net/U+2764 https://codepoints.net/U+FE0F,所以它是并不是说它们是4字节长的UTF-8字符。我确保MySQL的默认字符集是utf-8。)

有趣的是,我无法在本地开发人员环境中完全重现此错误。一个特别的区别是它只会发出异常警告。

UPDATE1:

这仍然困扰着我:

mysql> SELECT default_character_set_name FROM information_schema.SCHEMATA WHERE schema_name="sblive";
+----------------------------+
| default_character_set_name |
+----------------------------+
| latin1                     |
+----------------------------+
1 row in set (0.00 sec)

我将特定表格的字符集转换为utf-8:

mysql> alter table uploads_uploads convert to character set utf8 COLLATE utf8_general_ci;
Query OK, 1209036 rows affected (1 min 10.31 sec)
Records: 1209036  Duplicates: 0  Warnings: 0

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

mysql> SHOW VARIABLES LIKE '%char%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)

mysql> SHOW VARIABLES LIKE '%colla%';
+----------------------+-------------------+
| Variable_name        | Value             |
+----------------------+-------------------+
| collation_connection | utf8_general_ci   |
| collation_database   | latin1_swedish_ci |
| collation_server     | utf8_unicode_ci   |
+----------------------+-------------------+
3 rows in set (0.00 sec)

1 个答案:

答案 0 :(得分:1)

您要求❤️后跟"非间距" " VARIATION SELECTOR-16"。

  • 你的字节是utf8 - 好
  • 您的连接需要指定utf8 - 是吗?
  • 您的TEXT列需要声明CHARACTER SET utf8 - 是吗?使用SHOW CREATE TABLE进行验证。
  • 如果您使用的是HTML,则需要说charset=UTF-8 - 是吗?

如果'后端用户'建议您切换到utf8mb4。可能会输入更多表情符号 - 表情符号'需要它。

<强>附加物

让我们检查数据......请运行此

SELECT col, HEX(col) FROM ...

这两个字符应该提供十六进制E29DA4EFB88F。如果你看到C3A2C29DC2A4C3AFC2B8C28F,那么你有#34;双重编码&#34;,这是一个更麻烦的问题。我认为2764FE0F表示utf16。