Run app with a clean database everytime - android

I'm creating an app which uses a database.
I'm refining the database, fixing errors and so on. But the onCreate() method of my helper is called only once. So, after the first test, the app's still using the old, and wrong database.
I can implement the onUpgrade() method, but this seems odd to me, since I'm actually fixing errors and I'll find many of them. Is this the right way to do it? Playing with database version numbers?
Is there any simpler method?

When your app starts, you can physically delete the database:
File myDB = getDatabasePath("my_database.db");
myDB.delete();
The next call you make to your database ContentProvider will then create the database from scratch.

If your are testing/creating your app, then just delete your database in onCreate() method on your Activity:
this.deleteDatabase("your_database_name");
But for the future release of your published app, i suggest to learn and implement the method onUpgrade().

Related

Program which will run only once and perform SQLite database set up on Android

Am a newbie to android development. I would like to have suggestions for setting up database during installation and populate the data in the tables.
As tables creation, data population in tables is a one time process which will be done during App installation, how can we write a program which will meet this requirement.
Thanks in advance!
You cannot setup database during installation. You can however do one of two things:
Ship your application with a ready made database.
Have your database created for you the first time it is needed.
If you prefer to go with option 1, you should extend an application class and have your databases created for you on the onCreate() call there. Refer to this for help. If you want to use option 2, you should understand the database lifecycle, most important thing being, when you are trying to access your database but it is not there, the system calls the onCreate() method of the class that extends SQLiteOpenHelper class. Refer to this url.

Deleting the database when an Account is removed

My app uses the SyncAdapter pattern, holding user credentials using the AccountManager and a ContentProvider to store data in a db.
When the account gets removed I can remove the db using the approach explained in this question. The db gets removed by doing:
boolean deleted = mContext.deleteDatabase(DatabaseHelper.DATABASE_NAME);
This works fine but when I do the login again everything is still there. It feels like the ContentProvider doesn't know that the db has been removed.
In this answer, inazaruk says:
You need to make sure you've killed the process that hosts
ContentProvider that uses that specific database file. And only than
delete it.
Killing the process to clear a db doesn't feel right.
Is there a better thing to do?
If I'd had to do that I would try it the following way:
add some Uri that when you insert or delete using that Uri triggers database deletion inside your ContentProvider. When deleting also clear all references to the SQLiteDatabase since it is possible that you can still access the old database file through that (If you delete a file in Linux and you have that file open you can still use it - it's just no longer accessible via the path).
By putting the deletion inside the ContentProvider you should be able to close the database connection and track the deletion state in a way that you know that you need to recreate the database file.
ContentProviders don't quit unless you kill your app so you probably have the same instance running and probably references to the old file as mentioned above

Strategy for updating Android app (with database)

Please forgive me if this question has been answered - I searched and couldn't find it.
I have an Android app that I want to upgrade, and it uses a SQLite Database. I want to update some of the application logic in the app, but there will be no updates to the database schema or contents. I basically need to keep the database exactly as-is for the user.
Do I need to do anything in onUpgrade to ensure that the database is kept, or can I leave the DB stuff alone for this update?
The onUpgrade() method is used incases of version change. Which means the database stored in the phone needs to be altered or dropped or deleted and a new database to be created. As your application does not have any of these requirements you can leave the DB stuff for this update.
This related article may help you with your question.
The way that I understand it, is that you need to put your database changing code in onUpdate() if you WANT to update between versions. But since you don't intend to, and are probably keeping the database version the same, then you will most likely have no issues at all.
Upgrading will NOT interfere with SQLite. Changes to db structure will not be implemented unless you programmatically do so (in onUpgrade method) or you uninstall and reinstall your app.
As long as it is the SAME application that you are upgrading, your db will not be affected and your data will not be affected either. If you change the signing key used in building your apk, your db will be recreated.
Conversely, if you change database structure at any given point, your onUpgrade method will come into play. You will be forced to backup, drop, recreate and repopulate tables which have been changed between versions (oher tables remain untouched both in structure and in data).
NOTE: In debugging, i just uninstall and reinstall the app every time i make db changes, but in production you DONT want to do that.

What happens to my Android app and database when a user updates through Market Place?

Ok, I currently have an app on the MarketPlace and today I release its first update. Will the SQLite database used by my app be overwritten by the update, thus calling the onCreate once again? Or will it simply not touch the database? What happens to the SharedPreferences?
Sorry for the seemingly simple questions, I've been googling for an hour now and haven't found anything on what happens to a package when the user updates the app. Does it totally wiped out and reinstalled? Dunno. Any help will be highly appreciated. Thank you!
if you are using sqlite inside your db adapter, it will check for this variable:
private static final int DATABASE_VERSION;
If it's different than the one installed in user's device, it will attempt to call the overridden onUpgrade() method of sqlite.

SQLiteOpenHelper, when is onCreate called

I'm toying with the Android SDK, with a view to writting a simple app for friends (and maybe for sale).
This app will search a database for keywords and dispaly the results on the screen, I've had a look at the searchabul dictonary and the notepad demo applications, but I'm still a bit unsure some things.
I know I need to write a class that extends the SQLiteOpenHelper, and use that to create the database, however how do I check if the database already exists?
Does onCreate get called on installation or every time an instance of the class is created? is the easyist way just to try and add the database each time and catch any errors (feels a little dangerous to be makeing the assumption every error will be due to the database already existing).
Thanks in Advance.
Your SQLiteOpenHelper will handle the creation of the database. Once the db has been created on the users phone SQliteHelper will be happy until you change the database version number.
As long as you put your db creation code in the onCreate method of a SQLiteOpenHelper you will be fine.
Have a look at the onUpgrade method from the Notepad demo as you need to write some code that handles what happens when your db tries to upgrade itself (it will do this when you change the db version number)
Whenever you call helper.getWritableDatabase() , onCreate method is called.

Categories

Resources