What is the best practice for updating the DB schema? I could send a text file with the SQL commands. The app could check the text file and execute the commands needed. I am guessing then I will have a flag to indicate that the update has been done. I havent found a way to delete a file in the asset folder from the开发者_开发知识库 app, which would be the best thing to do after the DB was updated.
Any thoughts on this?
You will probably want to override
onUpdate(SQLiteDatabase db,int old Version,int newVerison)
The following tutorial can walk you through the process: http://www.codeproject.com/KB/android/AndroidSQLite.aspx
What is the best practice for updating the DB schema?
Use SQLiteOpenHelper
. The only time you will be updating your schema is when you update the application. Whether you have the SQL commands in a file that you read in or just in your Java code is up to you.
I am guessing then I will have a flag to indicate that the update has been done.
That is part of what SQLiteOpenHelper
gives you.
I havent found a way to delete a file in the asset folder from the app, which would be the best thing to do after the DB was updated.
That is not possible, sorry.
As mentioned from others the SQLiteOpenHelper is the thing you are starting with.
Depending on your environment and requirements, Liquibase may be worth a look. It allows writing your update statements in a more structural way and is easier to use than plain SQL operations. I haven't used it for android, though.
If you want to update the db schema use the following code in your SQLiteOpenHelper extended class. This will update the table structure and never lose your saved data from db,
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
if (oldVersion < newVersion)
{
db.execSQL("ALTER TABLE " + "TABLE_NAME" + " TO " +
"NEW_TABLE_NAME");
db.execSQL("ALTER TABLE " + "TABLE_NAME" + " ADD COLUMN " +
"FIELD_NAME" + " INTEGER;");
}
}
SQLite doesn't support removing or modifying columns.
精彩评论