Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android rename SQLite Database

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.

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.

Clarification:

The old database will contain only one table that I need to compare values from against the new (from the update) database.

Both databases have been copied over from an sqlite file in the assets folder.

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.

like image 688
Somk Avatar asked Nov 20 '11 19:11

Somk


People also ask

How do I rename a SQLite database?

Syntax. To rename a table, the SQLite ALTER TABLE syntax is: ALTER TABLE table_name RENAME TO new_table_name; table_name.

How to remove column in SQLite?

DB Browser for SQLite allows you to add or drop columns. In the main view, tab Database Structure , click on the table name. A button Modify Table gets enabled, which opens a new window where you can select the column/field and remove it.

How do I edit a table in SQLite?

Summary. Use the ALTER TABLE statement to modify the structure of an existing table. Use ALTER TABLE table_name RENAME TO new_name statement to rename a table. Use ALTER TABLE table_name ADD COLUMN column_definition statement to add a column to a table.

Can we change column name in SQLite?

Use the ALTER TABLE RENAME COLUMN to rename a column in a table. If you are using SQLite 3.25. 0, you should upgrade it and use the new syntax.


2 Answers

Just rename the File. Make sure the database is closed first!

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 clarification. Rename the old db (as above), copy the new one from the assets folder, open both databases and do your compare. Then delete the old file.

like image 162
Kevin Galligan Avatar answered Oct 23 '22 17:10

Kevin Galligan


Lord Flash is right, you should delete the old db and copy the new one…

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
    }
}
like image 33
Renaud Avatar answered Oct 23 '22 18:10

Renaud