I am developing an android application which have
sq lite db on my device
server sql db on main server which sync them, with two identical
(same fields structure and fields name)
i need to develop synchronization between these db in 2 ways
If I change database data in Android then It will change data on web server
when i change data from sever then it should change data in android
sync will run in 1 day automatically
now the question is How do I sync those databases in Android? as i am newly entered in mobile world and i am internee also and they give said if i will done this work then i will be permanent.
found several things but i am at beginners level. i don't find any samples. i have found similar questions here but does not find any code , any sample which only sync data on 2 way.
i will appreciate if some one will give me (open source project) or some sample code with some explanation.
or something like that which will show me a way to do this.
How to sync SQLite database on Android phone with MySQL database on server?
Android bi-directional sqlite database synchronization
i have found these article but does not help me.
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 updating 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 have to tailor the above code according to your data returned from DB. hope it helps:)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With