我想将一个表中存在的所有行数据发送到服务器,我的表具有三个不同的数据行,但是当我使用Cursor时,它仅将最后一行数据添加三次。那么如何获取所有三行
JSONObject putjsonObjectTrans = new JSONObject();
try {
int count = dbHelper.getCountOfRows();
JSONObject invoiceDetail = new JSONObject();
SQLiteDatabase sqLiteDatabase=dbHelper.getReadableDatabase();
String query = "select * from " + TABLE_NAME;
JSONArray invoiceArray = new JSONArray();
Cursor cursor = sqLiteDatabase.rawQuery(query, null);
while (cursor.moveToNext())
{
/* for (int i = 0; i < count; i++)*/
invoiceDetail.put("ItemId", cursor.getInt(1));
invoiceDetail.put("ItemCode", cursor.getString(2));
invoiceDetail.put("ItemName", cursor.getString(3));
invoiceDetail.put("ItemQuantity", cursor.getDouble(4));
invoiceDetail.put("Rate", cursor.getDouble(5));
invoiceDetail.put("DiscAmount", cursor.getDouble(6));
invoiceDetail.put("DiscPercentage", cursor.getDouble(7));
invoiceArray.put(invoiceDetail);
}
putjsonObjectTrans.put("acc_no", acccNo);
putjsonObjectTrans.put("acc_name", accCustomerName);
putjsonObjectTrans.put("EntryType", getEntryTypeName);
putjsonObjectTrans.put("EntryDate", gSentryDAte);
putjsonObjectTrans.put("NetAmount",15.2);
putjsonObjectTrans.put("division_no", MainActivity.divisionID);
putjsonObjectTrans.put("invoicedtl", invoiceArray);
} catch (JSONException e) {
e.printStackTrace();
}
答案 0 :(得分:1)
更改代码为此
JSONObject putjsonObjectTrans = new JSONObject();
try {
int count = dbHelper.getCountOfRows();
SQLiteDatabase sqLiteDatabase=dbHelper.getReadableDatabase();
String query = "select * from " + TABLE_NAME;
JSONArray invoiceArray = new JSONArray();
Cursor cursor = sqLiteDatabase.rawQuery(query, null);
while (cursor.moveToNext())
{
/* for (int i = 0; i < count; i++)*/
JSONObject invoiceDetail = new JSONObject();
invoiceDetail.put("ItemId", cursor.getInt(1));
invoiceDetail.put("ItemCode", cursor.getString(2));
invoiceDetail.put("ItemName", cursor.getString(3));
invoiceDetail.put("ItemQuantity", cursor.getDouble(4));
invoiceDetail.put("Rate", cursor.getDouble(5));
invoiceDetail.put("DiscAmount", cursor.getDouble(6));
invoiceDetail.put("DiscPercentage", cursor.getDouble(7));
invoiceArray.put(invoiceDetail);
}
putjsonObjectTrans.put("acc_no", acccNo);
putjsonObjectTrans.put("acc_name", accCustomerName);
putjsonObjectTrans.put("EntryType", getEntryTypeName);
putjsonObjectTrans.put("EntryDate", gSentryDAte);
putjsonObjectTrans.put("NetAmount",15.2);
putjsonObjectTrans.put("division_no", MainActivity.divisionID);
putjsonObjectTrans.put("invoicedtl", invoiceArray);
} catch (JSONException e) {
e.printStackTrace();
}
答案 1 :(得分:1)
在while循环内的行下面写
JSONObject invoiceDetail = new JSONObject();