Android-将SQLite与MySQL同步的最佳方法 [英] Android - Best way to sync SQLite with MySQL

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

问题描述

我正在一个包含Web应用程序和移动应用程序的项目中,该项目记录了每日用户的数据.用户可以删除,更新其数据,并且可以使用许多设备来插入数据.
我打算以此方式开发:
用户输入他们的数据,然后插入到SQLite.服务将定期(每5小时或每秒钟)启动,以使用时间戳与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中获取您想要与后端同步的数据,并使用凌空(volley)通过JsonObjectRequest发送,例如您的请求看起来像这样

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.
    });

u可以添加一个新值,该值指示该值是否已从本地数据库同步或否.例如,假设您有一个名为student的表,并且此表包含三列,分别为ID,NAME和当您的响应返回成功时在上面的代码中同步的那一行已同步的列更新为true | false,表示该行是否与您的后端同步了或者没有.并从数据库中获取数据,您应该执行以下操作.

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同步的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆