douju1997 2012-03-30 21:24
浏览 72
已采纳

将值从android sqlite3发布到sql数据库以进行同步操作

I am trying to upload values from an android sqlite3 database to a mysql database on my server. I have been successful in posting values from a single JSON object that I create. However, when I'm trying to sync values from my sqlite3 database, mysql database on server is not getting updated. Here is my php code on the server.

<?php  
mysql_connect('localhost','sapeksha_me','getmein'); 

$json = file_get_contents('php://input');
$obj = json_decode($json);

mysql_select_db("sapeksha_locationdata");

mysql_query("INSERT INTO location (MobileID, Latitude, Longitude, Speed, Acceleration, Time, Date, Sync) VALUES ('".$obj->{'MobileID'}."', '".$obj->{'Latitude'}."', '".$obj->{'Longitude'}."', '".$obj->{'Speed'}."', '".$obj->{'Acceleration'}."', '".$obj->{'Time'}."', '".$obj->{'Date'}."', '".$obj->{'Sync'}."')");

?>

And here is the code snippet for posting the content from my sqlite3 database to the server.

        SQLiteDatabase db = databasehelper.getWritableDatabase();
        Cursor cursor = db.query(TABLE, null, null, null, null, null, null, null);

        cursor.moveToFirst();
        while(cursor.isAfterLast() == false) {

            if(cursor.getString(cursor.getColumnIndex("Sync")).equals("yes") ) {

                String mob = cursor.getString(cursor.getColumnIndex("MobileID"));
                String lat = cursor.getString(cursor.getColumnIndex("Latitude"));
                String lng = cursor.getString(cursor.getColumnIndex("Longitude"));
                String speed = cursor.getString(cursor.getColumnIndex("Speed"));
                String acc = cursor.getString(cursor.getColumnIndex("Acceleration"));
                String date = cursor.getString(cursor.getColumnIndex("Date"));
                String time = cursor.getString(cursor.getColumnIndex("Time"));
                String update = cursor.getString(cursor.getColumnIndex("Sync"));

                JSONObject json = new JSONObject();
                try {
                    json.put("MobileID", mob);
                    json.put("Latitude", lat);
                    json.put("Longitude", lng);
                    json.put("Speed", speed);
                    json.put("Acceleration", acc);
                    json.put("Time", time);
                    json.put("Date", date);
                    json.put("Sync", update);
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    receive = HttpPostExample.SendJsonUpdate(json, Sync_URL);
                } catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Toast.makeText(context,  receive,
                        Toast.LENGTH_SHORT).show();
            }
            cursor.moveToNext();    
        }
        cursor.close();

The code snippet using HttpPost..

public static String SendJsonUpdate(JSONObject json, String URL) throws ClientProtocolException, IOException {


    try {
        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, TIMEOUT_MILLISEC);
        HttpConnectionParams.setSoTimeout(params, TIMEOUT_MILLISEC);

        HttpClient client = new DefaultHttpClient(params);


        HttpPost post = new HttpPost(URL);

        post.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
        post.setHeader("json", json.toString());
        StringEntity se;
        se = new StringEntity(json.toString());

        post.setEntity(se);
        post.setHeader("Accept", "application/json");
        post.setHeader("Content-type", "application/json");
        post.setHeader("Accept-Encoding", "gzip");

        long t = System.currentTimeMillis();
        HttpResponse response = (HttpResponse) client.execute(post);
        Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]");

        HttpEntity entity = response.getEntity();

        if(entity != null) {

            InputStream instream = entity.getContent();
            Header contentEncoding = response.getFirstHeader("Content-Encoding");
            if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                instream = new GZIPInputStream(instream);
            }

            String resultString = convertStreamToString(instream);
            instream.close();

            Log.i("Read from server", resultString);

            return resultString;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;

}        

Logcat is showing the "HttpResponse received in []" statements, yet the database on the server is not getting updated. I have not still gained experience in android development and php/sql coding. The code might not be the best way of doing it but what am I doing wrong?

  • 写回答

1条回答 默认 最新

  • douchi0471 2012-03-30 21:45
    关注

    Is your auto-commit turned on?

    mysql_query('SET AUTOCOMMIT=1');
    

    If not, wrap your statements in

    mysql_query('START TRANSACTION');
    <your queries>
    mysql_query('COMMIT');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效