java Android中sqlite和sql server同步(crud操作)两种方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14453577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sync in Android sqlite and sql server (crud operation) in two ways
提问by Syed Ali
I am developing an android application which have
我正在开发一个 android 应用程序,它有
- 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.
- 我设备上的 sq lite db
- 主服务器上的服务器 sql db 同步它们,具有两个相同的
(相同的字段结构和字段名称) - 我需要以两种方式在这些数据库之间开发同步
- 如果我在 Android 中更改数据库数据,那么它将更改 Web 服务器上的数据
- 当我从服务器更改数据时,它应该更改 android 中的数据
同步将在 1 天内自动运行
现在的问题是如何在 Android 中同步这些数据库?因为我刚进入移动世界,我也是被拘禁者,他们说如果我能完成这项工作,那么我将是永久的。
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.
发现了几件事,但我处于初学者水平。我没有找到任何样品。我在这里发现了类似的问题,但没有找到任何代码,任何仅以 2 路同步数据的示例。
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 手机上的 SQLite 数据库与服务器上的 MySQL 数据库同步?
Android bi-directional sqlite database synchronization
i have found these article but does not help me.
我找到了这些文章,但对我没有帮助。
回答by R9J
Use this as your android service
将其用作您的 android 服务
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
此类帮助您更新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:)
您必须根据从 DB 返回的数据来定制上述代码。希望能帮助到你:)