根据 Gabe Sechan 的评论,最简单的方法是每次启动应用程序时从资产文件夹中复制数据库,即更改:-
private void createDB(){
boolean dbExist = DBExists();
if(!dbExist){
this.getReadableDatabase();
copyDBFromResource();
}
dbSglite=getReadableDatabase();
}
to :-
private void createDB(){
copyDBFromResource();
dbSglite=getReadableDatabase();
}
您显然对评论有疑虑
“那不是每次活动启动时都会复制数据库吗?”
是的,会的(那会那么糟糕吗?-修辞的).
但是,假设您要使用数据库版本,即根据当前版本检查资产文件夹中的版本。您基本上仍然需要从资产文件夹访问数据库,因此您将对照另一个数据库检查一个数据库(至少打开它们)。所以仍然会有一些开销。
一个可能不太密集的选项是检查资产文件的上一次更改日期与共享首选项中最后复制的资产文件的日期相对应。 (File
lastModified
方法)文件 - 上次修改时间.
从类似的角度来看,另一个选择是检查包version针对最后实施的,再次存储在共享首选项中。包信息 - 版本代码.
当然,在这两个选项中,仅当存在差异(增加)时才会从资产文件中替换数据库。
使用包版本的示例
以下示例(在dbHelper如果包版本增加(或者数据库不存在),类)将从资产中复制数据库:-
class dbHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "questions.db";
private static final int SCHEMA_VERSION = 1;
private static final String SHARED_PREFS = "shared_prefs";
private static final String SHARED_PREFKEY_QUESTIONSDBLASTUPDATED = "spkey_qdblastupdated";
public SQLiteDatabase dbSglite;
private String mDBPath;
private final Context myContext;
public dbHelper(Context context) {
super(context, DATABASE_NAME, null, SCHEMA_VERSION);
this.myContext=context;
this.mDBPath=context.getDatabasePath(DATABASE_NAME).getParent();
}
@Override
public void onCreate(SQLiteDatabase db){
Log.d("ONCREATE","OnCreate Method Called.");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void createDatabase(){
createDB();
}
private void createDB(){
if (isQuestionsDBNew() || (!DBExists())) {
Log.d("COPYFROMASSET", "Copying Questions From Assets");
copyDBFromResource();
setQuestionsDBNew(getPackageVersion());
} else {
Log.d("COPYFROMASSET",
"Questions not copied from Assets - New Questions Check result was " +
Boolean.toString(isQuestionsDBNew()) +
" DB Exists result was " + Boolean.toString(DBExists())
);
}
dbSglite=getReadableDatabase();
}
private boolean DBExists(){
SQLiteDatabase db = null;
try {
String databasePath = myContext.getDatabasePath(DATABASE_NAME).getPath();
db = SQLiteDatabase.openDatabase(databasePath,null, SQLiteDatabase.OPEN_READWRITE);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
db.setVersion(1);
} catch (SQLiteException e) {
Log.e("SqlHelper", "database not found");
}
if (db != null) {
db.close();
}
return db != null;
}
private void copyDBFromResource() {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = myContext.getAssets().open(DATABASE_NAME);
File databasedir = new File(myContext.getDatabasePath(DATABASE_NAME).getParent());
databasedir.mkdirs();
outputStream = new FileOutputStream(mDBPath+"/"+DATABASE_NAME);
byte[] buffer = new byte[1024];
int length;
while ((length=inputStream.read(buffer))>0){
outputStream.write(buffer, 0, length);
}
outputStream.flush();
outputStream.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
throw new Error("Problem copying database.");
}
}
public void openDataBase() throws SQLException {
String myPath = myContext.getDatabasePath(DATABASE_NAME).getPath();
dbSglite = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READWRITE);
}
private boolean isQuestionsDBNew() {
SharedPreferences prefs = myContext.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE);
long stored_lastused = prefs.getLong(SHARED_PREFKEY_QUESTIONSDBLASTUPDATED,-1);
Log.d("NEWQUESTIONS?", "Result of testing package version " +
String.valueOf(stored_lastused) +
" against " +
String.valueOf( getPackageVersion()) +
" was " + String.valueOf(stored_lastused < getPackageVersion()));
return (stored_lastused < getPackageVersion());
}
private long getPackageVersion() {
PackageInfo pi;
try {
pi = myContext.getPackageManager().getPackageInfo(myContext.getPackageName(),0);
} catch (PackageManager.NameNotFoundException e) {
return -1;
}
Log.d("PACKAGEVERSION", "The version of package " +
myContext.getPackageName() +
" was " +
String.valueOf(pi.versionCode)
);
return pi.versionCode;
}
private void setQuestionsDBNew(long lastused) {
SharedPreferences.Editor editor = myContext.getSharedPreferences(SHARED_PREFS, Context.MODE_PRIVATE).edit();
editor.putLong(SHARED_PREFKEY_QUESTIONSDBLASTUPDATED,lastused);
editor.apply();
}
}
Notes
- The code is very much based upon the code from the question. There are :-
- 用于共享首选项处理的两个附加类变量(常量)。
- 三种新方法:-
-
isQuestionsDBNew
如果包版本大于共享首选项中存储的版本,则返回 true(共享首选项中没有任何内容会导致 -1,因此应该小于任何包版本)。
-
getPackageVersion
返回长整型包版本。
-
setQuestionsDBNew
这会更新适用的共享首选项。
- 更改 createDB 以检查包版本更改,然后从资产复制数据库。还有一个额外的检查来查看数据库是否存在,尽管只有在删除数据库文件时才需要这样做。删除应用程序的数据将删除共享首选项,从而导致数据库被复制。
- 该代码包括我留下的一些诊断日志记录。
- 这还没有经过广泛的测试(即我还没有达到增加软件包版本的程度)。
示例的输出 - 正在安装的应用程序:-
01-05 19:46:44.849 26692-26692/m.com.so48103235_updateprepopdb D/PACKAGEVERSION: The version of package m.com.so48103235_updateprepopdb was 1
01-05 19:46:44.850 26692-26692/m.com.so48103235_updateprepopdb D/PACKAGEVERSION: The version of package m.com.so48103235_updateprepopdb was 1
01-05 19:46:44.850 26692-26692/m.com.so48103235_updateprepopdb D/NEWQUESTIONS?: Result of testing package version -1 against 1 was true
01-05 19:46:44.850 26692-26692/m.com.so48103235_updateprepopdb D/PACKAGEVERSION: The version of package m.com.so48103235_updateprepopdb was 1
01-05 19:46:44.850 26692-26692/m.com.so48103235_updateprepopdb D/COPYFROMASSET: Copying Questions From Assets
01-05 19:46:44.855 26692-26692/m.com.so48103235_updateprepopdb D/PACKAGEVERSION: The version of package m.com.so48103235_updateprepopdb was 1
示例的输出 - 后续运行:-
01-05 19:48:10.375 26755-26755/m.com.so48103235_updateprepopdb D/PACKAGEVERSION: The version of package m.com.so48103235_updateprepopdb was 1
01-05 19:48:10.376 26755-26755/m.com.so48103235_updateprepopdb D/PACKAGEVERSION: The version of package m.com.so48103235_updateprepopdb was 1
01-05 19:48:10.376 26755-26755/m.com.so48103235_updateprepopdb D/NEWQUESTIONS?: Result of testing package version 1 against 1 was false
01-05 19:48:10.376 26755-26755/m.com.so48103235_updateprepopdb D/PACKAGEVERSION: The version of package m.com.so48103235_updateprepopdb was 1
01-05 19:48:10.381 26755-26755/m.com.so48103235_updateprepopdb D/PACKAGEVERSION: The version of package m.com.so48103235_updateprepopdb was 1
01-05 19:48:10.381 26755-26755/m.com.so48103235_updateprepopdb D/PACKAGEVERSION: The version of package m.com.so48103235_updateprepopdb was 1
01-05 19:48:10.381 26755-26755/m.com.so48103235_updateprepopdb D/NEWQUESTIONS?: Result of testing package version 1 against 1 was false
01-05 19:48:10.382 26755-26755/m.com.so48103235_updateprepopdb D/PACKAGEVERSION: The version of package m.com.so48103235_updateprepopdb was 1
01-05 19:48:10.387 26755-26755/m.com.so48103235_updateprepopdb D/COPYFROMASSET: Questions not copied from Assets - New Questions Check result was false DB Exists result was true
- 由于报告数据库未复制而再次调用添加日志消息的方法时使用了更广泛的消息,因此产生了额外的消息。