So I got a sqlite database (version 7) which stores data for my App (version 1.0) and I want to fix a few bugs (that have nothing to do with my database). What happens if I update my App to version 1.1? will the data be deleted? or will it just be deleted if i update the databse version to 8?
(My onUpgrade in the databasehandler will delete the existing databse and create a new one by the way)
Upgrading an app does not do anything to the database. Old data files are kept intact.
Changing the version number in code for your SQLiteOpenHelper will cause onUpgrade() to be called if the version number in the database file is lower. That by itself doesn't delete anything, but if you yourself delete the data (as you say), then it is lost.
See also: When is SQLiteOpenHelper onCreate() / onUpgrade() run?
Data will delete if you change the version, but in onUpgrade method we have two parameters
called oldVersion and newVersion. we can do what ever we want by checking old new version
condition.
Related
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.
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.
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 an app receiving its data from a database in the assets folder. Now everyone has the app on their phones I want to update the database. I have the new database ready to go on a server, but how to I get the updated database into the assets folder on their phones. All only if they want to update of course???
Cheers,
Mike.
SQLiteOpenHelper constructor has an version argument. If you supply some number that is bigger than the existing database version in phones memory, onUpgrade function will run after getWritableDatabase is called. Inside onUpgrade you should handle updating your database.
Some example code here:
Question about onUpgrade method android
I have a database that I built in SQLite browser, and it works fine. I launched an app with a prebuilt database and now I want to add more tables and data to that database.
I can get the app to launch the onUpgrade() method of the SQLiteOpenHelper. But the problem is, it's doing that EVERY time I use the helper.
I have it localized to, only on app launch, separating the upgrade command from the helper I used to retrieve data, but this is still a problem.
I have figured it out though, as I have been using the same database on my computer (the one that I'm editing) since version 1. So, whenever it writes the newer database onto the SD card it's showing version 1 even though I should be up to version 4 by now.
So, my question is, how can I manually edit the database version of the original database so that when it updates it isn't writing the old version number over the new one?
To manually update the version to 4 you execute the following SQL statement:
PRAGMA user_version = 4
Another way to change the version of your Sqlite Database. You can use DB Browser for SQLite:
Open the database file with "DB Browser for SQLite".
Change the "User Version" number to whatever number you want
Click the "Save" button
You can also set it via the setVersion SqlLiteDatabase method.
Source: http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#setVersion(int)