Skip to content Skip to sidebar Skip to footer

How To Pass Sqlite Table Values Row Wise Into Json?

Hi I pass the Data base values into webservices. but it passing only resent entry valules only. I need to pass all rows one by one into JSON. Can any one help me with sample code.

Solution 1:

SQLite in DB

Cursor cursor;
     cursor = myDataBase.rawQuery(" SELECT " + VENDORID + " FROM  " + VENDORINFO_TABLE
                + " WHERE " + CATEGORYID + "  = " + id,null);
        while (cursor.moveToNext())
       {

         id = cursor.getInt(cursor.getColumnIndex(VENDORID));

          // your JSON put Code JSONObject jsMain = newJSONObject();
        try {
            jsMain.put("email", id);
            return jsMain.toString();
        } catch (JSONException e) {
            e.printStackTrace();
            return"";
        }
}
    cursor.close(); 

Solution 2:

check below code try this way. this is just advice for you.

Cursor cus = db.selectAll_delete();
            JSONObject jon1 = newJSONObject();
            JSONArray jaa = newJSONArray();
            int i = 0;
            try {
                if (cus.moveToFirst()) {
                    do {
                        String a = cus.getString(1);
                        // Toast.makeText(this, ""+ a,// Toast.LENGTH_LONG).show();JSONObject job_get = getData_b(a);

                        jaa.put(i, job_get);
                        i++;
                    } while (cus.moveToNext());
                }

                jon1.accumulate("details", jaa);
            } catch (Exception e) {
                // TODO: handle exception
            }
              if (cus != null && !cus.isClosed()) {
          cus.close();
                 }

            String js = jon1.toString();
            send_to_server(js);
            Log.d("test json", "" + js);

method getData_b();

privateJSONObjectgetData_b(String a) {
                // TODO Auto-generated method stubCursor cus = db.fetchRow(a);
                JSONObject job = newJSONObject();
                try {

                    if (cus.moveToFirst()) {
                        do {
                            job.put("B", cus.getInt(1));
                            job.put("I", cus.getString(2));
                            job.put("O", cus.getString(3));
                            job.put("D", cus.getString(4));
                            job.put("D u", cus.getString(5));
                            job.put("L", cus.getString(6));
                            job.put("B", cus.getString(7));
                            job.put("A", cus.getString(8));
                            job.put("K", cus.getString(9));
                            job.put("A", cus.getString(10));
                            job.put("Time", cus.getString(11));
                            job.put("Upd", cus.getString(12));
                            job.put("Deleted", cus.getString(13));

                        } while (cus.moveToNext());
                    }

                } catch (Exception e) {
                    // TODO: handle exception
                }
                return job;
            }

Post a Comment for "How To Pass Sqlite Table Values Row Wise Into Json?"