Android SQLite database lifecycle - android

I'm new to android. I want to ask about SQLite database lifecycle. Basicaly, I want to include the insert data method and viewing data method in the main class (which basicaly called 1st when we open the app). Then I'm going to make some update to the database in another activity. What I want to ask is, when I'm going to open the app for the next time, which data that will be showed? Is it the 1st data that I inserted with insert method OR the updated one?

I don't understand exactly your question. So I hope this will answer it. There are 4 operations, you should implement: create, read, update, delete. For example when you have a contact table with id, name, phone number then you can create (John, 012345). If you then call the read method with id 1, you will get (John, 012345). But if you update this before you read it, you obviously get the updated item - the database didn't delete anything when you close the activity. I highly recommend you to create a new class to handle those crud-operations, because you will get crazy, when you wanna change the update operation and must search it in every single line from your app. I also recommend you to read this blog. It saved me a lot of time:
http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

As far as I understand your question,
my answer is -> You should see the updated data, because Db is same throughout the app.

The data that will be shown is the updated one , because SQL operations are immediate. It is very unlikely that onUpdate() will not update the database until and unless it encounters any error. you can see that itself using a CursorLoader attached with your cursor after running a query command. This can be done in an activity just displaying the contents of the database.
For database operations, It is beneficial to use Content Providers cause then you can extend your apps to widgets, share data with other apps etc.

Related

which approach is better to manipulate database in android

In my android app i am performing sql data extensively where i need to download the data from server , store it into database and populating it on the ListView.
moreover, i need to perform the database search and filter the data in a ListView too.
so far in my past projects i have used simple method to manipulate database like manually opening & closing database , getting data from database using cursor and storing it in a array list and populating over ListView.
after exploring i came across the ContentResolver and LoaderManager and tried few samples too but i am not able to decide what to use where i need to perform more database operation with search in a ListView.
I would even love to know in which scenario what approach is preferable.
i am waiting for commonsware comments too ;)
ContentResolver/ContentProvider is meant for providing data to other applications. If you aren't going to do that, then using it is clunky and overkill. It really doesn't provide any value, and even Google's own docs suggest not using it for inside of your app.
LoaderManager- meh. The idea of a Loader is useful, and you're probably already using it- a loader is just the idea of reading large amounts of data from your DB on an AsyncTask. LoaderManager will provide some efficiency gains if you're being restarted due to configuration changes, but there's other ways to achieve them. It isn't a bad thing to use, but it will save you minimal to no work- you still have to write a custom CursorLoader to make your db call, and you'll have some code to manage the loader manager. You aren't wrong to use it, I'm just not convinced of its value.

Suggestions on how to use SQLiteDatabase wisely for Android

I'm thinking of making an app that involves, using a bit of data that is not too large, putting it into an SQLiteDatabase and doing simple operations with the data such as put them into a ListView or display a certain piece of data etc...
So I don't really know that best way of doing this so I've got a few questions to ask.
Is it wise to store my data in a file in my assets folder then extract the data from the file and put it into the SQLiteDatabase?
Is it better to just have the data hard-coded into execute statements when I create my Database?
Should I be using an AsyncTask to populate my SQLiteDatabase with the data I have?
If question 3 is yes, how do I run my Activity such that if I were to simply display the data in my database (in a textview), that the method I call from my Database class that helps me display the data, does so only when the database has been populated (when the AsyncTask has finished).
I hope these questions are clear enough!
Both 1 & 2 approaches work flawlessly.
If you want to take approach #1 I really really recommend using Jeff Gifelt's Sqllite asset helper. I found quite easy generating a sqllite db outside the app and embedding it.
Generally speaking, the SQLiteOpenHelper's onCreate method is created the first time you try to read / write to your db, which is something you always should be doing in another thread. The doc says:
The database is not actually created or opened until one of getWritableDatabase() or getReadableDatabase() is called.
So the answer is: you should not bother about when the db is created, what you should care about is reading / writing to it from a background thread / asynctask.

Populating ContentProvider on application startup

I have an application where a certain data (about 100 rows) has to be stored in db before every start of application. I do not want to insert it every time when user starts app. What is the solution? How to store datas? What is the best way?
The onCreate method is the one that is run once by every app on its launch. Here you can have your code to insert the values in the DB.
Also you can connect to your DB and see if it already exists and if not perform the creation and insertion.
Refer these link's for more idea on DB in Android...
http://www.codeproject.com/Articles/119293/Using-SQLite-Database-with-Android

Using Database to call objects

I need some advice. I want to implement a set up, which has 4 static saved objects saved in a database. How would a user select one of these objects to use in a service... When using a SQLDatabase, do you have to create a database every time the app starts.
Any advice welcome.
Thanks
When the application starts for the first time the database is created.
The SQLite database saves the information you need into the database, and isnt erased until you allow the user to delete it or update the database.
You can find a very good tutorial on using a database here
EDIT:
It has means for you to allow the user to update the database also, such as information,names,numbers,ect.

Clearing Application database

I have an Android application which uses sqlite database to store textual data for a particular user. The data is read/written from/to the database from a service that runs periodically after every n seconds. My requirement is to clear data depending on age. Like, if the file is more than 24 hours old it should be deleted.
Checking the age of the file seems easy, just compare current time with the File creation time. The problem is:
where should I put this check and delete the file; inside application onCreate() or when the user logs in/ logs out? What would be an ideal place to trigger this logic from? Should I schedule an Alarm when the user logs in?
Should I delete the file or simply delete the rows? If I don't trigger the Cache clearance logic from login/logout, won't deleting the file cause problems, especially if the service is still trying to read and write from the database?
Please advice.
Thanks.
Well, this all depends on your logic for the application for the second part. I cant see why you would delete a database unless its just used to store temp data that does not matter. Either way the ideal place to do this check and delete is in the Data Access class thats opening the connection to the database. Below would be my logic...
Call to open DB
Check if DB file is old
If yes, delete it
Open Database (should create one if it does not exist)

Categories

Resources