0

Hope your doing well.

I'm developing and android app that should retreive data from the server side using JSON and store this data in SQL in android then display this data using Listview.

I will need to update the data located on the server side by adding or removing some rows.

The question is here, how can i insert ONLY the new row added on the server side into the SQL in android so it can be displayed in the Listview?

And how can i remove a row from SQL in android when removing it from database on the server so it can be not displayed in the Listview?

Thanks in Advance.

Anas Sherif
  • 81
  • 3
  • 9
  • so what have tried before ? – S.Thiongane Nov 11 '13 at 10:41
  • Seems like a bad idea to duplicate server data in a local database. How will you know when it's outdated? Anyway, do I understand correctly that your question is: how does that SQL work? – wvdz Nov 11 '13 at 10:41
  • If I didn't misunderstand your question, you want to do two-way server-client synchronization using JSON? – Andrew T. Nov 11 '13 at 10:49
  • If I understand you correctly, then [this question](http://stackoverflow.com/questions/9499849/synchronise-update-sqlite-databases) that I asked a year and a half ago is about what you're after. – Aleks G Nov 11 '13 at 11:05
  • @mansoulx I didn't try any approach so far. – Anas Sherif Nov 11 '13 at 11:40
  • @popovitsj I need to store the data in a local DB so the user can use the app ana access data even if there is no internet connection. – Anas Sherif Nov 11 '13 at 11:41
  • @antimo I need synchronization when needing updates on the DB on ther server , so the local DB is updated too. – Anas Sherif Nov 11 '13 at 11:43
  • @AleksG Your link is very helpful , but i don't know how to make modifications on the local DB , such as deleting some data from it after it is deleted from the DB on the server. Some times, i need to add or remove rows from the DB on the server, so when the user has an internet connection, he can click on a button to get the updated data without duplicating. – Anas Sherif Nov 11 '13 at 11:47
  • @AnasSherif You have to devise your database in such a way as to contain that information. For example, your server would send you a database with updates. There could be a table in it containing info of the changes. In your android app, you'd read that table and see that, for example, rows with IDs 1,3,16 have been deleted - so you delete them locally. Then you look at the actual table in that database and read the rows from there. For each of them, if the corresponding ID is not present in your existing table, you insert locally, if it does, then update locally. – Aleks G Nov 11 '13 at 11:51
  • Great, So i need a table on the server side to use to make changes. But when i use JSON, I send only data from one table, so i need to send data of this new table with the actual table in one link, Am i right ? – Anas Sherif Nov 11 '13 at 12:19

3 Answers3

0

create a removeItem and additem rest service. Now call the service when needed.

You can use gcm if you want to push changes from server to client.

hope you got my point, its simple and easy way to do.

Rajiv yadav
  • 823
  • 8
  • 24
  • Can you give me a sample of code so i can understand this approach ? I already need to use GCM to send notifications automatically when the server cnanges I used GCM to send notifications Manually by using PHP code but i can't use it automatically. – Anas Sherif Nov 11 '13 at 11:59
  • sorry i don;t have for for this. – Rajiv yadav Nov 11 '13 at 12:58
0

There are few times where a mobile database should aim to be 100% consistent with a server side database. Because of the nature of a mobile device, it is likely you cannot stay connected to your server all of the time.

With this in mind, Manually using an SQL setup to keep a replicated database (even a small on) on your device can become a nightmare pretty quickly (given timeouts, latency, locking, syncing etc).

If you truly need a replicated database, rather than a local database which you update as needed, you should investigate remote database technologies which promote eventual consistency - There are several API's available and even more 3rd party technologies (such as CouchBase) that offer solutions to this problem.

Try reading up here: http://developer.android.com/training/cloudsync/index.html

and here: http://developer.android.com/training/sync-adapters/index.html

Taking care of syncing SQL databases yourself may be more difficult than a single question can hope to answer.

Graeme
  • 25,714
  • 24
  • 124
  • 186
  • Good, But i want to make the App usable even if there is no internet connection, so i'm using local SQL because the changes will not be done more. So when there is an internet connection , the user can click on a button to get updated data from the server, the Update may be a new data or deleted data from existing data i have on the server. – Anas Sherif Nov 11 '13 at 11:58
0

Usually you implement some DAO to communicate with the database. Then you just call a method to get records.

Say, you have cars. Create a model class Car, then your ListView adapter should wait for a List<Car> cars; To get this list you do something like CarDao.findAll() which returns the list of cars.

In that findAll() method you can implement whatever logic you can think of. For example, get rows from the web server, write them to the local database (if needed), and select * from it. Or whatever. Just make sure you always return an up-to-date data (the list of cars in our case).

The same thing is with an updating a database. Calling CarDao.delete(id carId); should do the trick. Delete the row from the sqlite db, then send delete request to the server, then update you list view adapter from the local database.

Hope I got your question right.

Community
  • 1
  • 1
phil_g
  • 516
  • 6
  • 13
  • That's Great , this is what i have already done. But i need to change the data firstly on the server then the changes is made on the client side. Your example doing the vice , deleting from the local then from the server as i understood. – Anas Sherif Nov 11 '13 at 12:03
  • @AnasSherif it's the matter of positioning lines in your code - I mean, you manually send request to the remote server first, then you ask it to give you fresh data for your local database, update it, push into the list view adapter. – phil_g Nov 11 '13 at 13:04
  • Good, but in this case i need to know the id of the row i want to delete, so as Aleks G said in his answer, i need to send the information from the server. So in this example i need to send the IDs of the rows i want to delete from the server to the android app so i can use them to specify which row has to be deleted. Am i right ? or you mean something else? – Anas Sherif Nov 11 '13 at 13:53