MySQL throws Incorrect string value exception analysis
- 2020-05-14 05:04:27
- OfStack
Before, I thought it would be safe to use UTF-8 from top to bottom, but I still encountered character exceptions when fetching sina weibo data today.
Data caught from sina weibo is thrown an exception when stored:
Incorrect string value: '\xF0\x90\x8D\x83\xF0\x90...'
It was found that the characters causing the abnormality were not traditional characters but some kind of sutras... Oh my god... But the UTF-8 is supposed to be supportive. Isn't it omnipotent?
The original problem was mysql. If mysql is set to utf8, it can only support UTF-8 encoding of 3 bytes at most, while UTF-8 character of 4 bytes still exists, so if the utf8 character set you used to build the table is abnormal, it is natural that 1.
The solution is simple: change the character set of the field or table to utf8mb4.
More painfully, the character set utf8mb4 was not supported until after mysql 5.5.3.
Data caught from sina weibo is thrown an exception when stored:
Incorrect string value: '\xF0\x90\x8D\x83\xF0\x90...'
It was found that the characters causing the abnormality were not traditional characters but some kind of sutras... Oh my god... But the UTF-8 is supposed to be supportive. Isn't it omnipotent?
The original problem was mysql. If mysql is set to utf8, it can only support UTF-8 encoding of 3 bytes at most, while UTF-8 character of 4 bytes still exists, so if the utf8 character set you used to build the table is abnormal, it is natural that 1.
The solution is simple: change the character set of the field or table to utf8mb4.
More painfully, the character set utf8mb4 was not supported until after mysql 5.5.3.