从json解析中插入数据到sqlite数据库

问题描述:

我想在android数据解析表单中的json数组数据中插入数据到sqLite数据库。从json解析中插入数据到sqlite数据库

我的代码如下:

1)DBHelperClass - 数据库创建

public class DueAmountDataBHelper extends SQLiteOpenHelper { 

    public DueAmountDataBHelper(Context context) { 
     super(context, "abc.db", null, 1); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     String CREATE_TABLE_PRODUCT_DUE_AMT = 
       "create table due_amt_tab(" + 
         "shopId text primary key, " + 
         "shopName text NOT NULL, " + 
         "teluguName text NOT NULL, " + 
         "place text NOT NULL, " + 
         "dueAmount text NOT NULL " + 
         ")"; 
     db.execSQL(CREATE_TABLE_PRODUCT_DUE_AMT); 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

    } 

    public List<DueAmtDBModel> getShopdata() { 
     List<DueAmtDBModel> data = new ArrayList<>(); 
     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery("select * from due_amt_tab", null); 
     StringBuffer stringBuffer = new StringBuffer(); 
     DueAmtDBModel dataModel = null; 
     while (cursor.moveToNext()) { 
      dataModel = new DueAmtDBModel(); 
      String shopId, shopName, teluguName, place, dueAmount; 

      shopId = cursor.getString(cursor.getColumnIndexOrThrow("shopId")); 
      shopName = cursor.getString(cursor.getColumnIndexOrThrow("shopName")); 
      teluguName = cursor.getString(cursor.getColumnIndexOrThrow("teluguName")); 
      place = cursor.getString(cursor.getColumnIndexOrThrow("place")); 
      dueAmount = cursor.getString(cursor.getColumnIndexOrThrow("dueAmount")); 

      dataModel.setShopId(shopId); 
      dataModel.setShopName(shopName); 
      dataModel.setTeluguName(teluguName); 
      dataModel.setPlace(place); 
      dataModel.setDueAmount(dueAmount); 

      stringBuffer.append(dataModel); 
      data.add(dataModel); 
     } 
     return data; 
    } 
} 

这个表我需要插入此JSON数据
APi的 - http://demo4896782.mockable.io/shops

[ 
    { 
    "shopName": "Hello World.", 
    "shopTeluguName": "శరవాన గుడ్డు పంపిణీదారులు", 
    "shopAddress": "Bomanahalli", 
    "previousDues": 0, 
    "shopID": 1 
}, 
{ 
    "shopName": "Hello World.", 
    "shopTeluguName": "శరవాన గుడ్డు పంపిణీదారులు", 
    "shopAddress": "Bomanahalli", 
    "previousDues": 20, 
    "shopID": 2 
}, 
{ 
    "shopName": "Hello World.", 
    "shopTeluguName": "శరవాన గుడ్డు పంపిణీదారులు", 
    "shopAddress": "Bomanahalli", 
    "previousDues": 400, 
    "shopID": 3 
} 
] 

预先感谢您。

+0

或者您可以将'json-string'作为字符串存储在表中,然后使用模型类根据需要使用** Gson **库进行序列化或反序列化。 –

+0

我不能那样做,请示例程序。 –

下面的代码假设您可以解析服务器的json数据。

public void insert(JsonObject jsonObject){ 
    ContentValues values = new ContentValues(); 
    SQLiteDatabase db = this.getWritableDatabase(); 
    values.put("shopName", jsonObject.getString('Hello World')); 
    values.put("shopTeluguName", jsonObject.getString('shopTeluguName')); 
    values.put("shopAddress", jsonObject.getString('shopAddress')); 
    values.put("previousDues", jsonObject.getString('previousDues')); 
    values.put("shopID", jsonObject.getString('shopID')); 

    db.insert("YOUR TABLE NAME", null, values); 
} 

现在只需遍历JSON阵列,并呼吁在循环

首先这个功能,表结构应该是:

CREATE TABLE IF NOT EXISTS "TABLE_NAME" + "(" + JSON_STRING_KEY + " TEXT ")

现在,做一个模型类对于你从api获得的json数组。现在

,以插入此JSON-数组中的表,你可以做这样的事情:

void insertJsonArrayAsStringToTable(ArrayList<Model> modelList) { 
    SQLiteDatabase db = this.getWritableDatabase(); 
    db.delete(TABLE_NAME, null, null); // delete previous data 
    ContentValues contentValues = new ContentValues(); 
    // array list as a string; or you can directly put the string you got from 
    // onResponse(String response) of your volley StringRequest--if you're using it 
    contentValues.put(JSON_STRING_KEY, new Gson().toJsonTree(modelList).toString()); 
    db.insert(TABLE_NAME, null, contentValues); 
    db.close(); 
} 

到现在为止,我们已经从存储在表中作为API的JSON数组数据一个字符串。

现在,我们可以使用查询从表检索字符串并再次使用GSON到它转换成一个对象(这里的ModelArrayList):

public ArrayList<Model> loadData() { 
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor res = db.rawQuery("SELECT * FROM " + TABLE_NAME, null); // now where-clause, so we simply use null as an arguement 
    res.moveToFirst(); 
    ArrayList<Model> modelList= null; 
    while (!res.isAfterLast()) { // traverse the table 
     Type listType = new TypeToken<ArrayList<Model>>() { 
     }.getType(); 
     modelList= new Gson().fromJson(res.getString(res.getColumnIndex(JSON_STRING_KEY)), listType); 
     res.moveToNext(); 
    } 
    res.close(); 
    db.close(); 
    return modelList; 
} 

PS:如何你设法将来自API的其他可能的响应存储到数据库中,完全取决于你,无论是制作单独的表还是类似的东西。