Android updating SQLite database from IntentService - android

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?

Related

SQLite Updating tables with read only data to App users

I am new to the android development and having certain concerns below
(i)In my app i am using SQLite database in which user should only be able to read the data (so i am running only reading the db query in app code)
db = openOrCreateDatabase("PersonDB", Context.MODE_PRIVATE, null);
db.execSQL(SELECT * FROM persons);
(ii)To display fresh data(eg news etc) to users i need to update my tables every day but i am not sure how to do that ?
PS: I know the code of how to make users to insert data by using an android app but here in this case only i want to insert the data(not by the users of app)
Can someone help me on this one?
Thanks! in advance
You can implement a SyncAdapter with periodic sync that runs each day to update the database.
https://developer.android.com/training/sync-adapters/creating-sync-adapter.html
Also you if you can integrate your backed with Google cloud messaging (or the newer FireBase cloud messaging) to notify the app to retrieve fresh data when available
https://developers.google.com/cloud-messaging/android/start
In both cases, you'll retrieve data from the server and update the db within a Service so that the updates take place even if the app is not running.
UPDATE:
In case of SyncAdapter, the data transfer code can be implemented in the onPerformSync() method. As for updating the databse, SyncAdapter plays nice with ContentProvider which makes the CRUD operations easier.
So in that method, you'll fetch the data from the network, open the database for writing and insert/update new data records. the SyncAdapter has a getContext() method that you can use for any context operations.
You can also check this application I implemented, it has a SyncAdapter and ContentProvider.
https://github.com/MinaSamy/WeatherApp/blob/master/app/src/main/java/com/bloodstone/weather/sync/WeatherSyncAdapter.java

android sqlite update online with updating the app and affecting user inputted data

I using SQLite as my database.
Using the app, the user can save some item on the database.
But I would like also to update the database from time to time.
The problem is how can I update the database without affecting the user inserted data and in a manner that it will download the new database online and not by updating the app itself.
Since my comment is large, I'll post it as an answer.
You won't find any tutorial showing exactly what you're trying to accomplish, because what you need is somewhat complex (but not difficult)
First approach:
You need your app to query some webservice to find out if there's a newer version of the database, and if there is, download it
The database must be saved in some temporary location, then you transfer all the users saved data to the new database, and finally replace the apps database with the new database (already updated with user's data)
Another approach would be:
Make your app query some webservice to find if the database needs to be updated.
If yes, download the SQL commands to modify the database structure.

Android, Downloading Data and SQLite

I'm making an app which has to download some data, parse it and store it in a SQLite Database. However I'm having a problem where the downloadtask (an asynctask) executes everytime the app is launched and keeps appending the duplicate data to the database so I get multiple instances of the same data.
I only want to execute the download task if the database has values in it but still want to be able to run the downloadtask if the data file on the server is updated.
Other than checking if the number of rows in the table is greater than 0 how would I go about doing this? I'm not really sure what to search for. Any help would be appreciated!
Each reach should have a unique ID.
When loading the data check if the unique ID is in the database
If it exists update the row.
If it doesnt exist add it(append).
If you control the database in the website, you can put there a flag you update anytime the database is updated. So before reading the data, check your flag. If updated load new and append new data to the database.
For your local part of the app: Well you can easily update the data as you desires if a query on this database return more than one row. This is very easy using a cursor and getting its count after the query is executed. If you dont want to have duplicates just delete the database after you have made sure that your download task has retrieved the data from the remote source. In that way you won't have duplicates and you will be sure that you don't delete by accident the database if something goes wrong.
For the remote source:You can enable Push Notifications and send one after something is changed in the database.So by sending a notification you can trigger a service that will download the new data and parse it properly.

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

Syncing online database data with local sqlite database android

I couldn't really find information on this particular problem anywhere even though I know there is a lot of questions and documentation on sqlite so sorry if this is a duplicate.
Anyways, I'm developing an app which logs a user in and performs actions on a mysql database on my website via php scripts. But so that the user doesn't have to wait for a web response for every button they press/every activity launch, I have a sqlite database attached to the app which stores the live information as the app is being used (It uses GPS tracking so this data is stored as well as other things).
I suppose my question is, how much should I store locally to be uploaded later? Should I try and do everything locally after initially logging the user in and just uploading/syncing the data in the user's profile onPause() or something and make use of the lifecycle? Or should I do the opposite and try and do everything live online and get rid of the local sqlite database altogether?
Just wondering if anyone has had experience with this kind of situation and what conclusions they came to.
Thanks for your time,
Infinitifizz
There are some sqlite3-rdiff tools for syncing SQLite databases at mobigroup

Categories

Resources