I am working on updating the database on a Mobile device which is using SQLITE db, which should get updated as server updates it's database i.e wamp server.
Can anyone suggests me any ideas on how to achieve this.
I don't want to read the whole server database as it would increase the Data Usage while reading the whole database just for a single update or for multiple update.
Update is done in the product table and only the price field is updated by server side.
You can define a service in your app that periodically asks if there are updated data in your server db. On the server side you can implement a web-service that will receive a json object in which you will put the current date, the table name you want to check for updates and other info based on your purposes.
I'll explaine myself better with an example:
1) when the app starts a background service will also start. This service will query (for instance every 3 minutes) your webservice to see if there are new updates for a specified table.
2) The webservice will receive the table name you want to check and the datetime preferably in unix timestamp. For instance you want want to see if there are new records for the table "products" after 2012-08-20 22:00:00 . You can create a json object and a http request with this information within your app and pass it to the server side.
3) Your web-service will respond to you giving a json array with all the data that are being added or modified after 2012-08-20 22:00:00. Of course in the server db you have to store this information (basically each record will have a field with the datetime of first insertion / last modification)
4) You can then update your local sqlite database.
Probably this is not very efficient but it works.
Andrea
Related
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.
I need this for a custom app, built for a specific company, so it will not be on Google Play.
I have an app, that uses about 15 SQLite tables. It needs to be able to work offline and online, so I have to use SQLite to keep information for the offline part.
The main idea is that when user on Android touches a button, an update process is starting.
The logic of the update is:
For each local table I have a AsyncTask class dealing with the update process described bellow
Every local SQLite table have an "_id" field (autoincrement) AND "idremote" field to be filled with MySQL id of the record (plus the rest of the fields)
Each time the update starts, the app prepares a string containing all "idremote" id's from the local table and sends it to the server
On the server side, a PHP file receives the string of ID's and checks in the MySQL table, to see if there are new ID's that are not in the received String of ids (each AsyncTask has it's own php file on the server)
If the PHP finds in MySQL table new ids, then it sends the new records using JSON back to Android
I process the resulting JSON in onPostExecute of the AsyncTask and insert the new records in the SQLite table
So I have 15 AsyncTask classes that all perform the same operations as above, each of them dealing with a specific table.
I also do the update of the remote tables sending the new records to MySQL through the same mechanism
My problem is that I want to be able to see/know when the update is done/finished so I can notify the user of that fact, but since we are talking about multiple AsyncTasks ... that run simultaneously... I have no idea how to implement this. How to find out when all the AsyncTasks are done?
Or is there a better way to do this task? The update of the local/remote tables?
Thank you
The best way to do this would be using a SyncAdapter which is designed to handle syncing changes to a remote server in the background.
You can find more information here: http://developer.android.com/training/sync-adapters/index.html
You should ignore the section "Creating a Stub Content Provider" and create a content provider to access your existing SQLite database.
I have a SQLite database on Android, which I'm designing to be available offline, and is getting data through a REST API on the server side. But is there any way I can only get the data that has changed since last upload? For example, if there is 5 customers registered in the MySQL database, and one of them change the address, then I only want to update the one with new address, and not the other 4.
Using timestamp field will be a solution for downloading incremental changes from the server to the client. For uploading data from the client to the server - some "Dirty" flag can be used to designate new records for upload.
Have a look at this question: Client-server synchronization over REST, which addresses the similar problems.
There is no build in API.
However you can create a timestamp column for every table which gets set on every update and insert.
When syncing, you can ask the server to give you only the rows with a timestamp greater than the last sync.
You might get in trouble when doing updates in both directions.
I am making an android application that needs to update periodically an SQLlite database of strings. Can updation be done through the internet, if yes how ?
I have a few lists in my application which are filled by the SQLlite database. The contents of the lists needs to change every day. That's why I need to update it everyday.
Yes you can update your Sql Database from the internet. See Guide to Services.
You can fire an Intent Service which does the work on a separate thread and there you can make a connection (for example HTTP GET request) and then put the parsed data in your appropriate database.
If you are using Loaders to populate your list, the list will be updated when you store new data in your database.
You may take following approach:
maintain a date variable in the shared preference or sqlite and on application launch check that if todays date is greater than date stored then you can call the rest/soap based web-services and fetch the updated record from server and update your sqlite table.In turn your list will populate the latest data...use cursorAdapter for that
OR
you can implement a GCM(google cloud messaging) service on your server,which push the updated data from sever to client every day.
OR
every time you launc an application you can hit the web service and upadte your sqlite database.
I hope solution caters to your problem
HI All,
I am developing an application which would need to regularlay update the database from the server. I am planning to get the database from server in json format.
But I want to update the data for those records which have been updated on the server. I do not want to download the complete database but only the differential database so that data transfer is minimum each time.
What is the best way to achieve this? Any sample code available?
Rgds,
Sapan
Simply put a *modified_date* column in the corresponding tables in the database and ensure it is updated by server(or some other mechanism such as using database provided facilities) whenever a row is updated/added. At any time you can request the records which have *modified_date* greater than the time when last query was executed. On your application you have to maintain the time when the data was last fetched.