Android rename SQLite database

Can I rename a database already created in android?

In my application update, I would like to rename the old database and install a new one, then compare some values ​​and finally delete the old ones.

I am making a creation from a sqlite file in a resource folder. That is why I cannot rename all tables and insert new ones.

Clarification:

The old database will contain only one table, in which I need to compare the values ​​from the new one (from the update database).

Both databases were copied from the sqlite file in the resource folder.

As soon as I compare the values ​​from the old database with the new ones, I will delete the old one and use the new one in its place with the values ​​that I compared.

What I was thinking about was to rename the old one, create a new one in its place and do everything higher.

+5
source share
3 answers

Lord Flash is right, you must delete the old db and copy the new ...

Assuming you are using SQLiteOpenHelper, you can use the method createDatabaseIfRequired();in getReadableDatabase()andgetWritableDatabase()

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
    }
}
+3
source

Just rename the file. First make sure the database is closed!

Call this in your activity class:

private void renameDatabase()
{
    File databaseFile = getDatabasePath("yourdb.whatever");
    File oldDatabaseFile = new File(databaseFile.getParentFile(), "yourdb_old.whatever");

    databaseFile.renameTo(oldDatabaseFile);
}

Response to clarifications. Rename the old db (as indicated above), copy the new one from the resource folder, open both databases and compare them. Then delete the old file.

+7
source

sql .

, .

0

All Articles