Populating ContentProvider on application startup - android

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

Related

Android updating SQLite database from IntentService

I set my application up to store the users data in a SQLite database, and when they get a wifi connection a button appears on a screen refresh that lets them start an IntentService and upload the data to a AppEngine Datastore associated with the application. A result is returned from the datastore indicating success or failure. If successful the IntentService gets a reference to a SQLite database( with application context) and deletes the row. Most of the time this works well however if the user navigates out of the app, and the the app gets destroyed, the row data is not removed from the SQLite database.
I was thinking about switching to a ContentProvider, however I'm not sure if I can delete rows from the ContentProvider if the application is closed, which then wouldn't be any better than the SQLite database.
I could open the SQLite database with Intent Service Context, but then I wouldn't be able to use the singleton pattern, and I would have to deal with the database getting closed unexpectedly.
I could check the back-end to see if the data is present and then delete it on the front end if it is or upload it if it's not, but that doesn't seem like the most efficient way.
I'm wondering if anyone else has solved this type of problem and would like to share their solution with me?

Android SQLite database lifecycle

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.

One time insert, multiple reads into SQLite Android app

I know there are similar responses so I am going to make this very succinct. I am planning on developing an app which has 18 chapters and each chapter has 30 or 40 hymns. Now, Im planning on using an SQLite command, insert each hymn individually but after the insert, and after the APK file is generated, would the data on the database still be present? Or Does it need to inserted in on each install? What are my options?
If you use sqlite database for you app...every time the app is installed.. new database will be created(of course the old one will be deleted).. and so the hymns will be inserted on each install..(but once you install.. on running your app wont create new database and insertions..).. hope this is clear..
I am not Clear with your question..but if you r inserting data through your code..than on each installation your records would be inserted once..if you provide condition to do so for only once.
You have to code the insertion of the hymns at the start/launch of the application, so that the database is ready for retrieval for the application. But the next time, the application is started, check whether your database exists and has the hyms (size of database), if yes, dont populate the database again, if no, populate it. I hope you want to read from a file/array and insert the records to the database.Once you insert the records to the database, they are available for the reference until the application is uninstalled or the database is re-created. Sqlite database is a persistent storage. Now, Im planning on using an SQLite command, insert each hymn individually but after the insert, and after the APK file is generated, would the data on the database still be present? Yes it would be present. Once the application is installed, the code of database would be executed and the database would be created.
My suggestion to you is use the XML parsing to show the Hymens in place of sqllite. Simply create the xml file with the hymen tag then get the tag and show the data on screen.

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