我使用 SQLiteOpenHelper 从 assets 中读取数据库。它适用于除 android p 之外的所有 android 版本。
这是我的 copyDataBase 函数:
private static void copyDataBase() throws IOException {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
SharedPreferences shData = myContext.getSharedPreferences("data", 0);
SharedPreferences.Editor editor = shData.edit();
editor.putBoolean("databaseIsOnMemory", true);
editor.commit();
}
我在这个函数中输入了一个日志,它确实通过了它。但我收到此错误:
android.database.sqlite.SQLiteException: no such table: irancell (code 1 SQLITE_ERROR)
这就是我获取数据的方式:
c = myDataBase.query(true, Table_Name, new String[] {IDData,TextData},
null,null,null,null,null, null);
c.moveToFirst();
int i = 1;
while(!c.isAfterLast()){
list.add(new KharidBaste(c.getInt(0),c.getString(5)));
c.moveToNext();
i++;
}
并且错误在线:“c.moveToFirst();”