如何将SQLite表值逐行传递给JSON?

时间:2012-05-10 05:34:10

标签: android json sqlite

您好我将数据库值传递给Web服务。但它仅传递最近的条目值。我需要将所有行逐个传递给JSON。

任何人都可以帮我提供示例代码。

2 个答案:

答案 0 :(得分:1)

DBite中的SQLite

    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 = new JSONObject();
        try {
            jsMain.put("email", id);
            return jsMain.toString();
        } catch (JSONException e) {
            e.printStackTrace();
            return "";
        }
}
    cursor.close(); 

答案 1 :(得分:0)

检查下面的代码尝试这种方式。这只是给你的建议。

            Cursor cus = db.selectAll_delete();
            JSONObject jon1 = new JSONObject();
            JSONArray jaa = new JSONArray();
            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);

方法getData_b();

                 private JSONObject getData_b(String a) {
                // TODO Auto-generated method stub
                Cursor cus = db.fetchRow(a);
                JSONObject job = new JSONObject();
                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;
            }