Synchronization in sqlite and sql android server (crud operation) in two ways

I am developing an Android application that has

  • sqlite db on my device
  • sql db server on the master server that syncs them with two identical (same field structure and field name)
  • I need to develop synchronization between these db in two ways.
  • If I change the data of the database in Android then it changes the data on the webserver
  • when i change data from server then it should change data in android
  • synchronization will be performed after 1 day automatically

    now the question arises, how to sync these databases in Android? since I recently entered the mobile world and I am also interned and they give, if I do this job then I will be permanent.

found a few things, but I'm at a beginner level. I don't find any samples. I found similar questions here but didn't find any code, no sample, that only syncs data in 2 ways.

I would be grateful if someone gives me (open source project) or some sample code with some explanation.

or something similar that will show me a way to do it.

How to sync SQLite database on Android phone with MySQL database on server?

Database Synchronization with Android Bi-directional SQL System

I found this article but didn't help me.

+3


source to share


1 answer


Use this as your Android service.

public class Connector {

HttpClient httpClient = null;
HttpPost httpPost = null;
HttpResponse httpResponse = null;
String serverResponse = null;

public String callService(String url){

    try{
        httpClient = new DefaultHttpClient();
        httpPost = new HttpPost(url);
        httpResponse = httpClient.execute(httpPost);
        //Log.e("URL Response", serverResponse);
        serverResponse = EntityUtils.toString(httpResponse.getEntity());
    }
    catch(Exception ex){
        Log.e("Exception in connectivity", ex.toString());
    }
    //Log.e("URL Response", serverResponse);
    return serverResponse;
}

}

      

This class helps you update android db



public class DbSync extends SQLiteOpenHelper {

private static String DB_PATH = "";
private static final String DATABASE_NAME = "Your_Db_Name";
private SQLiteDatabase sqliteDb = null;
private String path = null;

public DbSync(Context context) {
    super(context, DATABASE_NAME, null, 1);
    DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
    path = DB_PATH + DATABASE_NAME;
}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

public void syncUsers(){
    try {
        String response = new TaskAsync().execute("URL_Where_Service_Running").get();
        Log.d("response", response);

        String tableName = "Table_Name";

        sqliteDb = SQLiteDatabase.openDatabase(path, null,  SQLiteDatabase.OPEN_READWRITE);
        sqliteDb.delete(tableName, null, null);

                    //here you have to parse your data from DB and insert using 
                    ContentValues values = new ContentValues();
        values.put("col_name1", parsedColumnValue);
        values.put("col_name2", parsedColumnValue);

        long status = sqliteDb.insert(tableName, " ", values);

        Log.d("database", Long.toString(status));


        sqliteDb.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

      

You should adapt the above code according to your data returned from the DB. hope this helps :)

+5


source







All Articles