将值从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?

dongshuo1257
dongshuo1257 服务器日志中有关于帖子的内容吗?
8 年多之前 回复

1个回答



您的自动提交是否已开启?</ p>

  mysql_query('SET AUTOCOMMIT =  1'); 
</ code> </ pre>

如果没有,请将您的语句包装在</ p>

  mysql_query('START TRANSACTION');

&lt;您的查询&gt;
mysql_query('COMMIT');
</ code> </ pre>
</ div>

展开原文

原文

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');

dongwen9947
dongwen9947 它是一个还是多个并不重要。 如果自动提交不正确并且您不发出手动提交,则不会将任何内容写入数据库。
8 年多之前 回复
doubiantiao4994
doubiantiao4994 现在,我包含了“开始事务”和“提交”命令,它现在可以使用了。 谢谢! 但是,有人可以解释为什么我需要拥有它们,即使我只有声明吗?
8 年多之前 回复
douhu7807
douhu7807 我从未使用过Transaction和Commit语句。 但是,当我只有一个查询语句时,这不是不必要的吗?
8 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐