Android Save object as blob in sqlite

后端 未结 2 1224
轻奢々
轻奢々 2020-12-24 04:12

is this right. I have this gridview. I want to save its item as blob in sqlite. so when i open the list of save data in sqlite ill just load the save item

2条回答
  •  自闭症患者
    2020-12-24 04:39

    The way I store data as BLOB in my DB, is by converting them to JSON and then storing the bytes. e.g.

    ArrayList persons  = new ArrayList<>();
    Gson gson = new Gson();
    ContentValues values = new ContentValues();
    values.put(MyProvider.KEY_DATA, gson.toJson(persons).getBytes());
    // insert or update the DB
    

    And to get the list back

    byte[] blob = cursor.getBlob(cursor.getColumnIndex(MyProvider.KEY_DATA));
    String json = new String(blob);
    Gson gson = new Gson();
    ArrayList persons = gson.fromJson(json, new TypeToken>()
                                     {}.getType());
    

    Edit: to answer your last question, you should store your data (list of items).

提交回复
热议问题