Keeping a DB updated - Android - android

With your help, I have successfully created a web page which has its own MySQL DB and then uses a JSON web service to pass the values my Android application.
The next stage is to copy them into a local SQLite DB so the phone does not require an internet connection to view the list.
I have successfully implemented code that creates a table, and inserts values into this table.
The problem is, I don't want it to necessarily re-write the whole DB at a time, which is the current idea to keep it current.
I might have things in the DB deleted or more things added, and want the local DB to reflect this.
What steps can I take to delete things that are no longer present in the MySQL DB in the SQLite one, and add new things?
Cheers

The best way to do this should be to have a date time column in your database and then just fetch the posts that are newer then the last time you fetched from the database, in your android app you could save an date time in your preferences
I'm sorry I missed that you wanted to delete items as well, than this probably isn't the best approach. Are you using the mySQL only for the API? Then you could probably mark posts as deleted inserted odd feeling them.

You can think this approach, parse your JSON response into objects of your data model and then create the content values holding that data model to work with the database in your device.
Once you have the content values, use a content resolver to work with your database and try an update of each data parsed from the JSON response (each content value), update method of the content resolver will return an integer value that tell us the number of rows updated in the database, if such number is 0 means that no data were in the database, so make an insertion, simply.
In that way you first search if the current data is present in the database, if it is, update it, if not, insert it.

Related

How to structure local database for deleting rows when syncing from server

I currently have a local SQLite database that will update periodically from a server.
Initially, the local database will be empty. The app will fetch entire server database as json, and insert all the new rows locally.
After some time the app will fetch the entire server database as json again, and insert any new rows, or update any existing rows that have changed.
Now, the issue is if the server's database has removed an item (row). The app will never be aware of these removed items, and it will exist forever locally.
What's the best way to handle this?
A few ideas:
Add a "is deleted" flag on each item. This would allow the app to remove any items, but this seems like it would be harder to maintain, and a lot of changes for each table. Although it would allow you to undelete items as well.
Same idea as the flag, just modify as existing field and return it. For example, return a negative id ( id = 150 -> id = -150 ). This wouldn't require any database changes, but is confusing to anyone who doesn't know what's happening.
Modify the result to return a list of "deleted" items. This wouldn't be too much work, but doesn't seem like a good solution either.
Is there any standard pattern to follow for this?
Thanks.
You can use Realm instead of SQLite for better experience, And When the app is fetching the data from server clear all data in the realm and insert new records, It will not save the removed item.
Thank you.

Database Sync Cloud from MySql to Sqlite Android

I have a remote database that can change at any time. I need to find a way to keep my sqlite database update with all the changes in the better and optimised way possible.
I thought having a single timestamp per table and send only the updated table to the client would be one solution, or maybe having one timestamp per row and sending only the updated row to the client..
But
- how can i manage deleted items, for example?
- how can i manage the technical update on Android?
Basically, in a few words, at some point in the app, i need to download the changes with an API and update the local db.
Does anyone have some ideas?
Thanks
Since it seems this is a one-way only sync, I would:
Make each table on the server DB have a UUID column and a last
updated date/time column.
Create a REST call that will request updated data. Ideally, it would
take the time the client was updated. This date should come from the
server during the update call.
The server would scan each table searching for rows that have a date > than the date passed. Serialize the data and return it as JSON along with the server date/time for the next request.
Now tracking deletes is a bit more work. You can either:
Never delete data and only mark the rows as deleted, this is the easiest, but depending on your data may end up with lots of rows in the DB.
Delete the data and then track the deleted rows in another table. These can be deleted after some time and if/when all clients have been updated.
Have a second update call that will push down all the UUID values and time stamps for each row. The client could then figure out which rows need to be deleted.
Though it's the most complex, I'd probably opt for option 3, as I don't like old data hanging around. The direction I would go would also depend on how many clients will be syncing with the DB.
I've decided to follow another approach:
I've created an api that parse the Mysql db into an SQLITE db on the SERVER side. There is a "last_update_timestap" that will be updated every time that something will be changed.
Another api call, gives in the header an extra field containing that timestamp.
On the client side there is a Sync process that do the follow when needed:
Api call retrieving just the HEADER, getting the last update timestamp and checking if an update is needed;
If is needed download the database from the other api
Write the database into a temporary file and copy that temporary file at the position and with the same name of the real database
Use the database
Thanks everyone for the help but this turned out to be the suitable approach for my project structure.

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.

Downloading data/content into Sqlite database

I want users to be able to get additional content from my website which means I will insert the downloaded data into the device's SQLite. I am wondering if I am approaching this the right way..
My current approach is to create a REST web service which returns data in JSON format, parse the JSON and insert it row by row into the Sqlite db on the android device.
Is this the right approach? Will it be too slow if there are many table rows to be inserted at one time? Or is there a way to download another SQlite db and merge it with the local one?
I welcome any suggestion, thank you in advance for your answer.
I works, but you absolutely need to paginate : set a limit to the number of element sent by your rest service.
Another approach would be to download the complete sqlite database file at once, but that requires some tweaks. see http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ (it is about embeding the database from the assets, but the preparation of the database is the same.)
A last point: large amount of insert, as well as downloading data from a server, must to be done in a separate thread or asynctask, from a service (not an activity that can be interrupted), or even better from a SynchronizationAdapter, which is called by the system itself.

Categories

Resources