This question already has answers here:
When does SQLiteOpenHelper onCreate() / onUpgrade() run?
(15 answers)
Closed 6 years ago.
I don't know very much about SQLite so,
i want to know what is use of onCreate() and onUpgrade() method of SQLiteOpenHelper.
And when the onCreate() and onUpgrade() method is called.?
Documentation
SQLiteOpenHelper onCreate() and onUpgrade() callbacks are invoked when the database is actually opened, for example by a call to getWritableDatabase(). The database is not opened when the database helper object itself is created.
SQLiteOpenHelper versions the database files. The version number is the int argument passed to the constructor. In the database file, the version number is stored in PRAGMA user_version.
onCreate() is only run when the database file did not exist and was just created. If onCreate() returns successfully (doesn't throw an exception), the database is assumed to be created with the requested version number. As an implication, you should not catch SQLExceptions in onCreate() yourself.
onUpgrade() is only called when the database file exists but the stored version number is lower than requested in constructor. The onUpgrade() should update the table schema to the requested version.
When changing the table schema in code (onCreate()), you should make sure the database is updated. Two main approaches:
Delete the old database file so that onCreate() is run again. This is often preferred at development time where you have control over the installed versions and data loss is not an issue. Some ways to to delete the database file:
Uninstall the application. Use the application manager or adb uninstall your.package.name from shell.
Clear application data. Use the application manager.
Increment the database version so that onUpgrade() is invoked. This is slightly more complicated as more code is needed.
For development time schema upgrades where data loss is not an issue, you can just use execSQL("DROP TABLE IF EXISTS <tablename>") in to remove your existing tables and call onCreate() to recreate the database.
For released versions, you should implement data migration in onUpgrade() so your users don't lose their data.
onUpgrade() is called (you do not call it yourself) when version of your DB is changed which means underlying table structure changed etc.
oncreate(): called only once when database is created for the first time. It's used to create a table in a database.
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.
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.
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'm developing a sqlite database in my android application. I need it can grow easily, so I need that in future upgrades, I could change the database, etc.
I want to know if when I change the data base version in the sqlite creation method, it creates a new file of the database. If it does, then, in the onUpgrade I should migrate all the data, isn't it?
In conclusion, what onCreate does exactly? Does it create a new file of the database? Or does it modify the actually one?
I'm asking this because I dont want that the onCreate creates a new file... I want to alter the actual data base only.
Thanks
OnCreate is only called when a database needs to be created for the first time.
OnUpgrade is called if the database already exists and the version numbers do not match. You should use OnUpgrade to alter your database from the old version to the new version.
I am assuming you are referring to OnCreate method in SQLiteOpenHelper. It gets called when you request for a writable or readable database and the the database needs to be created. We do not call the oncreate method directly. So if a database is already existing and we request for a writeable or readable database --- no new database gets created.
Refer to these links for more information
http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html
http://developer.android.com/resources/tutorials/notepad/index.html