我在我的项目中使用了预先填充的数据库。我有一个创建的 .sql 基础并在第一次启动时复制它。基础是大 33mb。
private void copyDataBase() throws IOException {
InputStream externalDbStream = context.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream localDbStream = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = externalDbStream.read(buffer)) > 0) {
localDbStream.write(buffer, 0, bytesRead);
}
localDbStream.close();
externalDbStream.close();
}
Run Code Online (Sandbox Code Playgroud)
它适用于除 API 28 之外的不同 android 版本。 API 28 抛出“Caused by: android.database.sqlite.SQLiteException: no such table: phrases”异常:
Caused by: android.database.sqlite.SQLiteException: no such table: phrases (code 1 SQLITE_ERROR): , while compiling: select * from phrases where complexity …Run Code Online (Sandbox Code Playgroud)