問題描述
在注意到應用程序由于不正確的字符串值錯誤而傾向于丟棄隨機電子郵件后,我繼續并切換了許多文本列以使用 utf8
列字符集和默認列整理 (utf8_general_ci
) 以便它接受它們.這修復了大部分錯誤,并使應用程序在遇到非拉丁電子郵件時也不再出現 sql 錯誤.
盡管如此,一些電子郵件仍然導致程序命中不正確的字符串值錯誤:(Incorrect string value: '\xE4\xC5\xCC\xC9\xD3\xD8...' for column '內容'在第 1) 行
內容列是一個 MEDIUMTEXT
數據類型,它使用 utf8
列字符集和 utf8_general_ci
列整理.在此列中沒有我可以切換的標志.
請記住,除非絕對必要,否則我不想接觸甚至查看應用程序源代碼:
- 是什么導致了該錯誤?(是的,我知道電子郵件中充滿了隨機垃圾,但我認為 utf8 會非常寬松)
- 我該如何解決?
- 這種修復可能產生哪些影響?
我考慮的一件事是切換到打開二進制標志的 utf8 varchar([some large number]),但我對 MySQL 相當不熟悉,也不知道這樣的修復是否有意義.
"\xE4\xC5\xCC\xC9\xD3\xD8"
不是有效的 UTF-8.使用 Python 測試:
如果您正在尋找一種方法來避免數據庫中的解碼錯誤,cp1252 編碼(又名Windows-1252"又名Windows 西歐")是最寬松的編碼 - 每個字節值都是有效的代碼點.
當然它不會再理解真正的 UTF-8,也不會再理解任何其他非 cp1252 編碼,但聽起來你不太關心這個?
After noticing an application tended to discard random emails due to incorrect string value errors, I went though and switched many text columns to use the utf8
column charset and the default column collate (utf8_general_ci
) so that it would accept them. This fixed most of the errors, and made the application stop getting sql errors when it hit non-latin emails, too.
Despite this, some of the emails are still causing the program to hit incorrect string value errrors: (Incorrect string value: '\xE4\xC5\xCC\xC9\xD3\xD8...' for column 'contents' at row 1)
The contents column is a MEDIUMTEXT
datatybe which uses the utf8
column charset and the utf8_general_ci
column collate. There are no flags that I can toggle in this column.
Keeping in mind that I don't want to touch or even look at the application source code unless absolutely necessary:
- What is causing that error? (yes, I know the emails are full of random garbage, but I thought utf8 would be pretty permissive)
- How can I fix it?
- What are the likely effects of such a fix?
One thing I considered was switching to a utf8 varchar([some large number]) with the binary flag turned on, but I'm rather unfamiliar with MySQL, and have no idea if such a fix makes sense.
"\xE4\xC5\xCC\xC9\xD3\xD8"
isn't valid UTF-8. Tested using Python:
>>> "\xE4\xC5\xCC\xC9\xD3\xD8".decode("utf-8")
...
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-2: invalid data
If you're looking for a way to avoid decoding errors within the database, the cp1252 encoding (aka "Windows-1252" aka "Windows Western European") is the most permissive encoding there is - every byte value is a valid code point.
Of course it's not going to understand genuine UTF-8 any more, nor any other non-cp1252 encoding, but it sounds like you're not too concerned about that?
這篇關于如何修復“不正確的字符串值"錯誤?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!