与服务器数据库同步的Andr​​oid数据库 [英] Sync Android database with a server database

查看:226
本文介绍了与服务器数据库同步的Andr​​oid数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据库,一个Android应用程序。我需要同步他们的Web服务器上的两个相同(相同的字段结构和字段名)数据库。但首先,我需要寻找到另一个数据库的网络服务器,从它比同步这两个数据库,从第一个数据库的一些额外的数据拉一些信息。香港专业教育学院一直在努力了一段时间的Andr​​oid应用程序,现在我只是学到了足够多的PHP与MySQL构建一个Web应用程序与基本的CRUD。

I have an Android app with two databases. I need to sync them with two identical (same fields structure and fields name) databases on a web server. But first I need to look into another database on a webserver, pull some information from it and than sync those two databases with some additional data from the first database. Ive been working on the Android app for some time now and I just learned enough PHP with MySQL to build a web application with basic CRUD.

我有几个问题:

  • 我如何同步这些数据库中的Andr​​oid?

  • How do I sync those databases in Android?

在同步完成工作两者兼得?如果我改变在Android中数据库中的数据,我可以相应地改变他们的Web服务器上,反之亦然?

Can the synchronisation work both ways? If I change database data in Android, can I change them accordingly on web server and vice versa?

林工作的一个基本GUI为老年人,使他们能够使用Android手机。我的web应用程序应该增加接触,催进的Andr​​oid应用程序的数据库,但是当我在我的Andr​​oid应用程序中添加联系人或提醒manualy,我想看看那些在我的web应用程序,以及(均为单向同步)。

Im working on a basic GUI for older people so that they can use an Android phone. My web application should add contacts and reminders into databases in Android application, but when I add contact or reminder manualy in my Android application, I want to see those in my web application as well (both way sync).

感谢您! :)

推荐答案

我将分享如何同步的Andr​​oid应用程序,使用PHP文件的PHP服务器之间的数据,它比Web服务等更多的方便..它是给予同样的结果与同样的回报。

I will share how synchronize data between Android application and PHP server USING PHP files , it's more easy than web service etc.. and it's give the same result with the same return.

查询

* - > PHP端:*

  • 例如创建表结构:MyTableName:列名/密码
  • 创建一个PHP文件,从你的表名称查询数据

  • Create your Table Structure for example : MyTableName : columns Login / Password
  • Create a PHP file to query data from your table name

<?php
//To Connect to your Data base
  mysql_connect("localhost","root","");
  mysql_select_db("YourDataBaseName");
// To execute a query from Your table

  $sql=mysql_query("SELECT *  FROM `Your_Table_Name");

  while($row=mysql_fetch_assoc($sql))
  $output[]=$row;

 // Convert to Json format 
 print(json_encode($output));
 // mysql_close();
?>

  • 命名你的PHP文件,例如你的表名称:MyTableName.php

  • Name your PHP file as your table Name for example : MyTableName.php

    将这个文件放在根服务器目录中:例如在瓦帕服务器www目录

    Put this file in your root server directory : for example in Wamp server the www directory

    使用浏览器测试返回的数据,使用URL像 www.localhost / MyTableName.php ,通常你将所有查询 JSON格式的内容。

    Test the return data using a browser, using your URL like www.localhost/MyTableName.php, normally you will have all your query content in Json format.

    * - > Android的一面:*

    添加Internet权限在你的清单。

    Add Internet Permission in your manifest.

    在你的点击按钮同步放:

    Inside the click of your button Synchronize put :

    String lReturn=_myClientHtpp.readFromUrl(www.localhost/MyTableName.php);
    // It will return a json format (Like navigator)
    //Code to Clear your data from Sqlite data base  TABLE ....
    
     try {
     // Create your Json Array
     JSONArray lList=new JSONArray(lReturn);
     // Iterate the json arry to get each Json object
     for (int i = 0; i < lList.length(); i++) {
    
    JSONObject lObject=lList.getJSONObject(i);
    
    String lLogin=lObject.getString("Login");
    String lPWD=lObject.getString("PassWord");
    
    //Code to Insert in Sqlite data base .....
    }
    } catch (JSONException e) {
     e.printStackTrace();
    }
    

  • 更新

    * - > PHP端:*

    <?php
    //Connect to Database
      mysql_connect("localhost","root","");
      mysql_select_db("YourDataBase");
    
    
     $sql= mysql_query("INSERT INTO YourTableName (Login,PassWord) VALUES ('".$_REQUEST['PARAM_Login']."','".$_REQUEST['PARAM_PWD']."')");
    
     if($sql==1){
     echo "true";
     }else{
     echo "false";
     }
    
    // mysql_close();
    ?>
    

    • 将这个PHP文件在您的主服务器目录WWW,并将它命名为 Insert.php
    • 在使用浏览器,这个网址 www.localhost / insert.php测试你的插入? PARAM_LOGIN =测试及放大器; PARAM_PWD =测试
    • 在通常情况下,你会得到真,并在你的服务器表命名新 行与测试/测试将被添加。
      • Put this PHP file in your main server directory www, and name it to Insert.php
      • Test your Insert using browser with this URL www.localhost/insert.php?PARAM_LOGIN=test&PARAM_PWD=test
      • Typically you will get 'True' and in your server table name a new row with test / test will be added.
      • * - > Android的一面:*

        • 在你的添加按钮写的请点击:

        • Inside the click of your 'Add' button write :

            // List of params
        ArrayList<NameValuePair> lListOfParams=new ArrayList<NameValuePair>();
        
        // Param login
        BasicNameValuePair PARAM_Login=new BasicNameValuePair("PARAM_Login", "test from Android");
        // Param PWD
        BasicNameValuePair PARAM_PWd=new BasicNameValuePair("PARAM_PWD", "Test from Android");
        
        // Add params to List
        lListParams.add(PARAM_Login);
        lListParams.add(PARAM_PWd);
        
        // Execute the post method from your ClientHttp class
        boolean lIsInsert=_myClientHtpp.SendToUrl("www.localhost/insert.php", lListOfParams);
        
        if(lIsInsert)
            Toast.makeText(this, "Insert Success", Toast.LENGTH_LONG).show();
        else
            Toast.makeText(this, "Insert Error", Toast.LENGTH_LONG).show();
        

      • 如果您有自己的服务器替换每个域名本地主机。

      • If your have your own server replace localhost per your domain name.

        如果您使用的是仿真器,代替你的本地主机酒庄每
        名 例如您的实际IP地址的查询网址变为:   192.168.1.2/MyTableName.php

        If you are using emulator , replace your localhost domaine name per
        your actual IP address for example the query URL becomes : 192.168.1.2/MyTableName.php

        这篇关于与服务器数据库同步的Andr​​oid数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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