gpt4 book ai didi

java - sqlite android中的编码字符

转载 作者:行者123 更新时间:2023-11-30 09:26:04 25 4
gpt4 key购买 nike

你好,我正在从两个不同的文件中读取英语和南非荷兰语,并将其放入一个 sqlite 数据库中,例如南非荷兰语中的单词是正确的

下面是南非荷兰语

    'n slegte lot
'n voël die wilde diere van die prooi
'n trekvogel

============================================= ===========

        public class MainActivity extends Activity {

DbAdapterSDCard db;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

String array[] = { "afrikaans" };
for (int j = 0; j < array.length; j++) {
String filePath1 = "/sdcard/Dictionarys/english.txt";
String filePath2 = "/sdcard/Dictionarys/" + array[j] + ".txt";
String UTF8 = "utf8";
int BUFFER_SIZE = 8192;
int i = 0;
System.out.println(array[j]);
db = new DbAdapterSDCard(array[j]);
try {
db.open();
File file = new File(filePath3);
file.createNewFile();

BufferedReader br1 = new BufferedReader(new InputStreamReader(
new FileInputStream(filePath1),UTF8), BUFFER_SIZE);

BufferedReader br2 = new BufferedReader(new InputStreamReader(
new FileInputStream(filePath2), UTF8), BUFFER_SIZE);

String sCurrentLine1, sCurrentLine2;

while ((sCurrentLine1 = br1.readLine()) != null
&& ((sCurrentLine2 = br2.readLine()) != null)) {
db.insertTitle(sCurrentLine1, sCurrentLine2);
}
} catch (Exception e) {
e.printStackTrace();
}
try {

db.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

但是在sqlite中是这样插入的 enter image description here

两者都有编码UTF-8字符集......

谁能告诉我,我已经尝试了很多,但都没有成功

txt文件中的编码如下

enter image description here

现在如果我使用 utf-8 编码,字符将不会正确显示 :(

enter image description here

最佳答案

这是我的解决方案:首先,您必须清理您的应用程序,因为您在 apk 中进行的每次构建都会重新创建数据库,有时更改代码中的编码不会给您带来效果。

首先,您必须通过使用 adb 重定向到您的包名称来清理您的应用程序,即

~/android-sdks/platform-tools/./adb shell pm clear com.example.my.project

或者如果你在 android sdk 平台工具文件夹中

adb shell pm clear com.example.my.project

然后

adb shell am start -a android.intent.action.DELETE -d package:com.example.my.project

每次要对数据库进行更改时,请使用此命令。在使用 adb 命令之前,我做了一些更改并且没有看到任何效果。

第二个在从即文件中读取行时放入编码字符集(linux 示例):

private class DatabaseHelper extends SQLiteOpenHelper {

private static final String ENCODING = "UTF-8";
private Context context;

public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
//DATABASE_NAME is just a name of database

this.context = context;
}

@Override
public void onCreate(SQLiteDatabase db) {

db.execSQL(COMMAND_CREATE_TABLE);
//COMMAND_CREATE is just command to create table i.e.
//CREATE TABLE myTable ( _id int...

try {
InputStream inputStream = this.context.getResources().openRawResource(R.raw.my_base_file);
BufferedReader bufferReader;

if (inputStream != null) {
bufferReader = new BufferedReader(new InputStreamReader(inputStream, ENCODING));

String line = "";

while ((line = bufferReader.readLine()) != null) {
db.execSQL(COMMAND_INSERT_START + line + COMMAND_INSERT_END);
}

}
} catch (IOException e) {
e.printStackTrace();
}
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS myTable");
onCreate(db);
}

}

public Cursor fetchAllRows() {
SQLiteDatabase localSQLiteDatabase = this.sqlDatabase;
return localSQLiteDatabase.query("myTable", ROWS_COLUMNS_NAME, null, null, null, null, null);
}

最好在所有操作之前将包含数据的文件转换为 UTF-8。以下是如何执行此操作的示例代码(Linux 版本):

iconv -f WINDOWS-1250 -t UTF-8 -o convertedBase.csv originalBase.csv

如果你是windows用户你可以尝试使用notepad++

关于java - sqlite android中的编码字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15067078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com