How to efficiently sync Android database with web database - android

My android app database syncs with a central database on my web server. My problem is efficiently syncing it.
The table is set up as "id" (primary key), "town".
The android app wants all the ids of the people in a given town
My current process is:
get list of all ids for town "A"
set all existing records in Android database to "old"
add/update ids in the list to "current"
delete any records still set as "old"
The bottle neck comes in the add/update process which is:
for each id {
result = update WHERE _id = id
if (result == 0) {
add record
}
}
Is there a statement like WHERE _id IN(id1, id2, id3, id4 ....) that would act as add/update if already there??
Or am I going about it completely wrong?
Thanks

then why you updating the data, at the time of synch cant you delete all the data in android device and add the latest data from the server
i am saying this because it will flexible, first delete all and insert all(latest ones)

build a table on server side (ID, Town, UpdateTime)
add triger on UPDATE/INSERT to modify UpdateTime
store LastUpdate time on device with fx SharedPreferences
send LastUpdate to server
build JSON or XML with ID, Town from a table WHERE UpdateTime > LastUpdate +(place in JSON or XML) (CurrentTime GETDATE()
from DB)
do insert/updates in device db update, LastUpdate to CurrentTime

Related

Checking new row is inserted in table database and run some script

I want to send some notification to android app when new row is inserted in table database, but I'm having trouble to check if there is new row inserted or not.
Is there any function or query to check if new row inserted in table? or some logic to acquire that.
It depends on the database you are using.
For example in MySQL you can query the table information_schema.tables to get the latest update time.
https://dev.mysql.com/doc/refman/8.0/en/information-schema-tables-table.html
For postgres
Check if track commit timestamp is on
show track_commit_timestamp;
If it's off, set it to on in your postgresql.conf file and restart postgres.
Post that run the following to track the latest updates in your table
SELECT pg_xact_commit_timestamp(xmin), * FROM YOUR_TABLE_NAME;

Automatically Delete a mysql field

I am developing an android application which is connected to the online database developed using mysql. I have provided the user to enter a time slot (ex. 10.00AM - 12.00PM), Day(Tuesday). And what I need is to delete the field when current date matches to the day and passed the time slot. (ex.Tuesday && 12.01PM).
How can I to write a query to delete the field automatically when the day and time matches the current day and time.
Thanks in Advance.
Write a cron job that will run at the required time to do the following:
ALTER TABLE `tablename` DROP COLUMN `columnname`
following query works fine for delete the record based on current timestamp.
DELETE FROM table
WHERE datecolumn = date('Y-m-d h:i A'); //'2012-05-05 10:00PM'

Data synchronization between two or Android devices and website

I am building an app that allows users to insert data and synchronize with website. User can insert data on the website as well. There are two entity tables (T1 and T2) and one N-M relation table (TR).
Data Structure (it's just illustrative):
T1 (_id, name, modified)
T2 (_id, name, modified)
TR (t1_id, t2_id)
The problem I am facing is data synchronization of IDs. E.g. Device A1 and A2 are offline and record is inserted on both, with the ID = 1. After they are online sync starts and there is a conflict with IDs. I thought about introducing an extra column gid - something like global ID. So the structure would be:
T1 (_id, name, modified, gid)
T2 (_id, name, modified, gid)
TR (t1_id, t2_id, t1_gid, t2_gid)
Global ID would be assigned by website.
But I not sure whether this is a good approach or not (never done anything like this before and cannot tell if there will be any future problem).
You have to use additional ids, suppose network_id, generate all network_ids on the server and use local ids on devices (e.g. UUID). When you are sending create entity request server will generate a real id and return it to you, so you can update a local database with network_id. It is important to use network_id as main field and local_id only if you don't have network_id.

Sync android sqlite db and sql server in one way

I am developing an android application which have sqlite db on my device and sql server db on the main machine, the android device contain very small part of main db, so i don't want to replicate whole db. and I have my own webserver that upload/download data between databases,
my question is how I know which part of main database is changed to only download changed entries?
One usefull technique is using rowversion data type. You can see it as the server change number. You can get the current change number by using the MIN_ACTIVE_ROWVERSION() function. Then query from changes occurred form last downloaded change number to current change number and finally store the current change number as the last downloaded change number in the local SQLite database.
Something like this:
DECLARE #CurrentServerChange binary(8)
SET #CurrentServerChange = MIN_ACTIVE_ROWVERSION()
SELECT * FROM Table1
WHERE
RecordVersion >= #LastDownloadedChangeNumber
AND RecordVersion < #CurrentServerChange
SELECT * FROM Table3
WHERE
RecordVersion >= #LastDownloadedChangeNumber
AND RecordVersion < #CurrentServerChange
SELECT #CurrentServerChange AS CurrentServerChange
I assume RecordVersion columns are of type rowversion

Best way to structure a sync between a tablet and a database?

Just curious on the best practice on syncing data from a database to an android tablet.
Tables:
- Part1
- Part2
- Part3
- Part4
- Part5
Whenever I open the app on the tablet I grab the latest lists from the database, truncate the table, and re-add the records. Each table consists of 400 records. So it takes around 60.45 per table to grab the data from the server and add it. Since I have 5 tables it takes around 5 minutes. Is there a better way to achieve efficient syncing for what I am doing? After I grab the data from the server, instead of truncating the table I've tried checking if it exists firsts before adding it but that didn't help with the time.
What I am currently doing: I get the JSON list from the API server and truncate the table and add the rows back. Pretty time consuming with 5 tables of 500 records each.
libraryApp = (LibraryApp) act.getApplication();
List<Pair> technicians = getJsonData("get_technicians");
if(technicians.size() > 0) {
stiLibraryApp.getDataManager().emptyTechnicianTable(); // truncate current table
// add technicians back to database
for(Pair p : technicians) {
libraryApp.getDataManager().saveTechnician(new Technician((Integer) p.key(), (String) p.value()));
}
}
Given the limited information provided I would try the following:
(1) Have your server keep a record of when the table you are updating was last "put" on the server. I have no idea what backend language you are using so I cannot make a sugestion. But it should be really easy to keep a lastupdated timestamp.
With this timestamp you will be able to tell if the version of the table on your server is more recent than the version on your mobile device.
(2) Using an AsyncTask download the data you need. I am not sure if all 5 tables are in the same activity, in seperate activities, in fragments or something else. However, the general idea is as follows:
private class GetTableData extends AsyncTask<Void, Void, Void>{
#Override
protected Void doInBackground(){
//get data from your sever
return null;
}
protect void onPostExecute(Void result){
//update the table view if version on server is newer
}
You will place all your I/O methods, that is those that connect to your server and download data within doInBackground. You will place all methods that update the table view within onPostExecute. This seperation is necessary because while I/O functions must run in the background after Jellybean, views must be updated from the UI thread.
(3) Check the timestamp of what you downloaded. If what you downloaded is newer update your table. You can acomplish this by simply adding in a conditional statment to your onPostExecute function such that
if(lastDownloadTime < lastUpdatedOnServerTime){
//update view
}
Depending on how big the table files are you may want to add a function on your sever code that just returns the time the table was last updated. That way you can check the time it was last updated against the time you last downloaded the table. If the table was updated on the server after you downladed it you can proceed to download the new information.
That's the basic idea. You can adapt it to your own set up.

Categories

Resources