0

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
  • Is there anything in the server logs about the post? Commented Mar 30, 2012 at 21:28

1 Answer 1

2

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');
Sign up to request clarification or add additional context in comments.

3 Comments

I have never used Transaction and Commit statements. But isn't it unnecessary when I have just one query statement?
Now, I included the 'Start Transaction' and 'Commit' commands and it works now. Thank you! However, can anyone explain why I need to have them even though I had only statement?
It doesn't matter if it's one or multiple. If auto-commit is not true and you dont issue a manual commit, nothing gets written to the database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.