本文介绍了Android - 将 SQLite 与 MySQL 同步的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个项目,其中包含记录日常用户数据的网络应用和移动应用.用户可以删除、更新他们的数据,他们可以使用许多设备插入数据.
我打算这样开发:
用户输入他们的数据,然后插入到 SQLite.服务将定期启动(每 5 小时或 sth)以使用时间戳与 MySQL 同步.
我确实在互联网上使用服务和时间戳搜索了一个样本,但我什么也没找到.如果有示例或教程会很棒.
我是 Android 新手,我不知道开发这样的应用程序的最佳方法是什么.提前致谢.

I'm working on a project containing web app and mobile app which records daily user's data. User are able to delete, update their data and they can use many devices to insert data.
I intend to develop this way:
User enter their data and then insert to SQLite. A service will start periodically (each 5hr or sth) to sync with MySQL using timestamp.
I did search for a sample using service and timestamp on the internet but I found nothing. It would be great if there are a sample or tutorial.
I'm new at Android and I have no idea what is the best way to develop an app like this. Thanks in advance.

----编辑----
我考虑使用时间戳或同步.哪个更有效?

----EDIT----
I consider using timestamp or synced. Which is more effective?

推荐答案

你可以使用 google 的 volley 库或任何替代的 libraries,这取决于你想怎么做发送数据,最好的方法是您使用 JSON 让您的生活更轻松,从 sqlite 获取您喜欢与后端同步的数据并通过 JsonObjectRequest 发送使用 volley,例如您的请求可能如下所示

you could use volley library by google or any alternative libraries, it depends on how you want to send the data, the best approach is that you use JSON to make your life easier, get the data from sqlite that you like to sync with your backend and send it over JsonObjectRequest using volley, for example your request can look like this

jsonObjectRequest postForm = new JsonObjectRequest(Request.Method.POST, URL, YourJsonData,
    new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            // here you can get your response.
        }
    },
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            // here you can tell there is something went wrong.
        }
    });

您可以添加一个新值,该值指示该值是否已从您的本地数据库同步.例如,假设您有一个名为 student 的表,当您的响应返回成功时,该表有三列 ID、NAME 和在上面的代码中同步或者没有.要从数据库中获取数据,您应该执行以下操作.

u could add a new value which indicates whether the value has been sync or no from your local database. for example, lets say you have a table called student and this table has three columns which are ID, NAME and synced in above code when your response return success update that row synced column with true|false which indicates whether this row synced with your backend or no. and to get the data from your database you should do something like this.

public String getStudents() {
    List<Student> students = new ArrayList<Student>();
    String query = "SELECT * FROM student WHERE synced = 0";
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);
    if (cursor.moveToFirst()) {
        do {
            Student st = new Student();
            st.setId(cursor.getString(cursor.getColumnIndex(ID)));
            st.setName(cursor.getString(cursor.getColumnIndex(NAME)));
            st.setSynced(cursor.getInt(cursor.getColumnIndex(SYNCED)));
            students.add(st);
        } while (cursor.moveToNext());
    }
    db.close();
    return new Gson().toJson(students);
}

这篇关于Android - 将 SQLite 与 MySQL 同步的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 11:56