android sqlite 数据库中未正确保存像 Umlaute 这样的特殊字符

Special chars like Umlaute are not saved correctly in android sqlite database

我无法在 Android 中使用 sqlite 保存特殊字符,例如变音符号(例如 Ä、Ü、ö 等)。输入“ä”会导致“\u00E4”。

我已经尝试使用以下代码在数据库中保存字符串,但没有帮助:

ContentValues values = new ContentValues();

String content = comment.getContent().toString();
byte[] chars = content.getBytes("UTF-8");
String utf8Content = new String(chars, "UTF-8");

values.put(DBHandler.CONTENT,utf8Content);

试试这个

SQLiteDatabase.execSQL("PRAGMA ENCODING=UTF-8;");

或您需要的等效编码

据我所知,values.put 接受像 "ä" 这样的字符。由于某种原因,它可以在将其转换为 "\u00E4" 等 Unicode 字符后保存在 SQLite 中。在这种情况下,只需将其原样保存在 SQLite 中,并在检索该数据时再次将 "\u00E4" 转换为 "ä"。这是returns那些字符从Unicode字符转换的方法。

public String getCharacterFromUnicode (String unicodeChar){    
    String returnString = null
    try {
        byte[] utf8 = unicodeChar.getBytes("UTF-8");
       returnString = new String(utf8, "UTF-8");
    }catch (Exception ex){    
    }
    return returnString;
}

哪个 returns 你的 "\u00E4""ä"

更新:

如果您不确定哪些部分被转换为 Unicode 然后使用 Apache commons-lang 库来转义那些,

myString = org.apache.commons.lang.StringEscapeUtils.unescapeJava(myString);