在使用DelphiXE3和SQLite3進行程式開發時,如果使用原版的SQLiteTable3,當使用UTF8Decode函數之後,可能依然存在亂碼問題,一個表現就是,最後一個漢字顯示為框,後面跟一個問号,其他的漢字解碼正常。
後來跟蹤了一下SQLiteTable3的源代碼,從SQLite擷取的字元串資料是正确的,但是因為TSQLiteTable的構造函數在讀取SQLite傳回的UTF8字元串時使用了setstring函數,強行将資料字段進行了轉換,引起字元串長度出現錯誤,是以在UTF8Decode解碼時出現了漏解碼的問題。
這裡給出一種解決方案,修複了該Bug。
1.将TSQLiteTable的構造函數Create中的setstring行改為:
Var
strStr: String;
strValue: PWideChar;
//****************************************
strStr := UTF8Decode(AnsiString(ptrValue));
strValue := PWideChar(strStr);
setstring(thisStringValue^, strValue, strlen(strValue));
因為在delphixe3中,setstring函數的第二個參數是PChar,也就是PWideChar類型。
2.在面在調用時,不需要再UTF8Decode解碼