I publish an android app in app store. After a while I do some edits special in database and move on to next version. My problem is edits and changes in database is just in content and no table nor column are added into it. so the schema is stick and the content is changed. Must I call onUgrade? Should I not change the database version?
If you use SqliteAssetHelper , You can do like the document says :
Upgrades via overwrite
If you have a read-only database or do not care about user data loss, you can force users onto the latest version of the SQLite database each time the version number is incremented (overwriting the local database with the one in the assets) by calling the setForcedUpgrade() method in your SQLiteAsstHelper subclass constructor.
You can additionally pass an argument that is the version number below which the upgrade will be forced.
Note that this will overwrite an existing local database and all data within it.
void onUpgrade (SQLiteDatabase db,
int oldVersion,
int newVersion)
Called when the database needs to be upgraded. The implementation should use this method to drop tables, add tables, or do anything else it needs to upgrade to the new schema version.
I would like to suggest don't increase database version then not need to call DB onUpgrade methods. If have changed in the schema you should need to call onUpgrade(). And upgrade database version also.
Related
I would like to know if it is possible to recover the old database data after upgrading to new database version of Android SQLite?
Thank you.
You cannot get the previous version database data because now it is upgraded.
You can take backup of each version before doing upgrade
Refer this :
Backup/restore sqlite db in android
Not if any of the old data has been changed (unless you can easily determine what has been changed and you can undo the changes).
It is possible to change the version number back using PRAGMA user_version = ?, where ? is an integer representing the version number to be changed to. This literally only changes that value (Offset 60 in the DB header). user_version pragma. If changing the the actual stored version number, then you would have to consider the coded version number as used by the super call, if they differ then onUpgrade or onDownGrade would be called. That is the value as stored in the DB header is compared to the value provided in the code by the super call.
You could alternatively change the version number passed to the Database Helper (subclass of SQLiteOpenHelper) BUT this would result in an exception unless the onDownGrade method was Overridden.
However, this method is not abstract, so it is not mandatory for a
customer to implement it. If not overridden, default implementation
will reject downgrade and throws SQLiteException. onDowngrade
The version number, as stored in the database, has no influence itself over the data. It's really a convenience value and it just so happens that SQLiteOpenHelper makes use of the value.
The database itself is just a file to back it up you just copy the file to restore it you basically copy it back. However, implementing backup and restore will only be of use after it has been implemented.
I have a database stored in my assets folder that I built using SQLite Manager. Now, when I add new records to any table of the database, I have to uninstall and reinstall the app. What would I have to do to upgrade the database in the right way without uninstalling the app?
Note:
My onCreate() and onUpgrade() are empty because as I mentioned, the database is already built.
I call super(context, DB_NAME, null, 1); in my helper constructor, but changing the version would not do anything because onUpgrade() is empty.
To allow upgrading, you must implement onUpgrade and do whatever is necessary to convert the old version to the new version.
If you have the new database file in the assets folder, you must replace the old file.
This is not possible with SQLiteOpenHelper because it has an active transaction while onUpgrade is called, but when you are not actually using the automatic creation/versioning mechanism, there is not reason to use SQLiteOpenHelper in the first place.
Suppose I have an app installed on my device that works on an SQLite database that is created alongside the first installation do the app. Now, suppose I make some changes on a new release that involve some changes on the DB schema. Obviously, it would be desirable to make the user know that it's DB will be wiped if the new release is installed. How can I make this advertisement prior to installation so the user can take needed steps in order to lose its data?.
In short, is there a way to open a dialog advertising the user on possible data loss and letting him decide what to do?
Thanks in advance,
Jose
Your database should have a method for updating the database
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// do something to upgrade
}
But that isn't called by default, but when your app first tries to open the database. At that point, you should be to architect a dialog that instructs your user and either do the upgrade or not.
It's also possible to alter the user's database within that onUpgrade method. For example, a string like this:
private static final String ALTER_TABLE3 = "ALTER TABLE exercises ADD COLUMN exscore REAL DEFAULT 0.0";
could be used in onUpgrade like so:
if (oldVersion == 5) {
db.execSQL(ALTER_TABLE3);
// now copy all of the values from integer to real
setUpMoveRoutine = true;
}
I did an upgrade from v4 to v5 that needed new elements within a table. And then called a method to move data as needed.
You should be able to manage just about any changes in your database
You shouldn't need to wipe the database to update the app, it's not a great user experience to do this and considering the SQLite implementation in Android is designed to allow you to upgrade the database it's also unnecessary.
Just implement the onUpgrade method in your SQLite helper class and provide upgrade steps to move between the different versions of your database.
The ALTER TABLE statements that SQLite supports are pretty limited, but you can add columns, if you need to make more complex structure changes you may need to copy data to a temporary table drop/create your table and then insert again.
In my android app, I was using a standard SQLite database with a helper class that had 1 table with 3 columns. In the most recent update I had to add another column of to the table, but some users have reported crashes, which (judging by the stack trace) I think comes from the new version trying to read from a column that does not exist because the data is from the old version. How can I protect the users' data between updates short of a manual backup and restore?
Here is the link to the complete updated database class:
https://github.com/cjbrooks12/scripturememory/blob/working/src/com/caseybrooks/scripturememory/databases/VersesDatabase.java
SQLiteOpenHelper will handle the database versioning, you will just have to provide it with proper database version numbers and overridden callbacks. Looking at your code:
Your DB_VERSION is 1. When you change the database schema between released versions, you should increment this number. The version number is stored in the database file, and if the version provided in code is different from the one stored in file, onUpgrade() or onDowngrade() will be called accordingly. In your case, since the database file already exists, no onCreate() was called and since the version numbers matched, no upgrade was performed.
Your onUpgrade() drops the table and then recreates it. In some cases this might be ok, say, it's just a cached copy of data stored elsewhere, but usually as a user, I don't want an app upgrade to delete my data. Implement onUpgrade() so that it does the necessary schema modifications while preserving data. Some generic strategies for this:
If it's just adding some columns ALTER TABLE and put some suitable default values.
If it's more complex schema change, rename the old tables to temporary names, create new tables and then migrate data from the temp tables.
In any case, after onUpgrade() the database schema should be in the same shape it would be if onCreate() was called to create a new database, but with existing data preserved.
I have the following scenario:
My app is published with database version 4 to the customers.
I did some bugfixes and added some more features. This process also changed my models and thats why the database changed too.
How can I check what database version the customer has installed on his devices and migrate the old data to the new database? Is there an onUpgrade method or something like this in the ActiveAndroid Library?
After taking a deeper look into ActiveAndroid's sourcecode I found a solution.
ActiveAndroid can use sql-scripts located in your asset folder to migrate from one version to another.
It sorts all your sql files located in assets/migrations/ using a natural sorting algorithm:
Each SQL script which was found will be executed if its > oldVersion and <= newVersion
if you access your db via SQLiteOpenHelper and do proper db versioning than you can use it's onUpgrade method to run some code to update your db. Othewise you should do your custom solution.
I suggest that you create a class which extends SQLiteOpenHelper. When your database is opened through this class, it will automatically call the onUpgrade() method when necessary. The parameters to this method include the new and old version numbers of your database schema.
Whenever your schema changes you need to increment the database version number, either through Configuration or AA_DB_VERSION meta-data. If new classes are added, ActiveAndroid will automatically add them to the database. If you want to change something in an existing table however (e.g. add or delete a column), this is done using sql-scripts named <NewVersion>.sql, where NewVersion is the AA_DB_VERSION, in assets/migrations.
ActiveAndroid will execute a script if its filename is greater than the old database-version and smaller or equal to the new version.
Let’s assume you added a column color to the Items table. You now need to increase AA_DB_VERSION to 2 and provide a script 2.sql.
ALTER TABLE Items ADD COLUMN color INTEGER;