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
Related
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 Database in my application which is created by just coping the SQLite database file from the assets folder but now in the next release I want to update the database without losing the previous data.
Is there any Logic or way to copy the previous data and create a new database through SQLiteOpenHelper so that in near future I will not face this problem.
Thanks
Use this gist
In the OnUpgrade method, copy your previous data from the old database.
In the doUpgrade method, use your copied data to update the new database.
Previous data will be kept by default when users update to a new version. but you must keep in mind there will be new users never get a former version app. So you should add complete database in the assets folder.
According to phonegap's API documentation,
window.openDatabase returns a new Database object.
This method will create a new SQL Lite Database and return a Database
object. Use the Database Object to manipulate the data.
However, what I dont understand is what happens when you close the app and reopen it. What happens to the database we created. Isnt it persisted and if so how to we retrieve it in order to perform other operations on it? Any assistance would be appreciated.
Yes, the database is persisted. All you need to do is provide the same "name" to window.openDatabase to open the database you have previously created.
I have an app that creates a database and do some stuff. I am wondering if i upload a new db to a server and download it to the exact folder where the older one exists it will be overwritten and i am good to go? Or there will be a problem. Assuming it has the same name, same column names, etc. Of course i am reffering to sqlite.
In Android, when performing a database update you should be using onUpgrade inside of the SQLiteOpenHelper. One way of doing this is to download text files that include the sql instructions needed to modify the current database or update rows with new data. The reason you have to do this is because Android will only create the database once. After the initial creation the call to onCreate for the database will not occur.
Hi I am new to android app development and have no idea of SQLite database.
Are The tables we create and the records we enter to the SQLite database tables happening run time. That means once create tables and insert data, do we have to do the same process again when exit and open the app again?(Only the database, tables and records remain until we open the particular app).
Thank you
Anything you do to the database is persistent. It will contain exactly the same data after your app shuts down and is later restarted.
The database and tables will remain after the app exits.
You can create a class that extends SQLiteOpenHelper to help.
Override the onCreate() and onUpgrade() methods there. onCreate() will get called only when your db doesn't exist yet. You can then execute sql to create the tables etc. onUpgrade will get called when you pass a new version number to the super() constructor.