问题描述
是否可以重命名已在android中创建的数据库?
Is it possible to rename a database already created in android?
在我的应用程序更新中,我想重命名旧数据库并安装新数据然后比较一些值最后删除旧的。
On my apps update I would like to rename the old database and install a new then compare some values and finally delete the old.
我正在从assets文件夹中的sqlite文件进行创建。这就是为什么我不能重命名所有表并插入新表。
I am doing the creation from an sqlite file in the assets folder. This is why I cannot rename all the tables and insert the new ones.
澄清:
旧数据库将只包含一个表,我需要比较来自新(来自更新)数据库的值。
The old database will contain only one table that I need to compare values from against the new (from the update) database.
两个数据库都已从资产中的sqlite文件复制过来文件夹。
Both databases have been copied over from an sqlite file in the assets folder.
一旦我将旧数据库中的值与新数据库进行比较,我将删除旧数据库,并使用我比较的值在其位置使用new。
Once I have compared a values from the old database to new I will delete the old and use the new in its place with the values I compared.
我想做的是重命名旧的创建新的地方并做上面的一切。
What i was thinking of doing was rename the old create the new in its place and do everything above.
推荐答案
Lord Flash是对的,你应该删除旧的数据库并复制新数据库...
Lord Flash is right, you should delete the old db and copy the new one…
假设你使用 SQLiteOpenHelper
,您可以在 getReadableDatabase()
和 createDatabaseIfRequired(); 方法code> getWritableDatabase()
Assuming you use a SQLiteOpenHelper
, you could use a createDatabaseIfRequired();
method in getReadableDatabase()
and getWritableDatabase()
private boolean checkOldDatabase() {
Log.d(Constants.LOGTAG, "OperationDbHelper.checkDatabase");
File f = new File(DB_PATH + OLD_DB_NAME);
return f.exists();
}
public void createDatabaseIfRequired() throws IOException, SQLiteException {
if (!checkOldDatabase()) {
// do db comparison / delete old db / copy new db
}
}
这篇关于Android重命名SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!