Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving ArrayLists in SQLite databases

So I want to save an ordered set of double values, and I want to be able to insert, retrieve or delete any value from this easily. As of such, I'm using a an ArrayList, where I define a class called Doubles to store the double values.

How do I store this arraylist in a record in an SQLite database? I mean...what should the columns type be? Can it be done?

like image 715
Achint Avatar asked Apr 18 '11 13:04

Achint


1 Answers

You cannot insert ArrayList directly into Sqlite. Instead, you could use JSONObject (org.json.JSONObject) to insert the ArrayList. Please check below snippet, you can try something like below....

To insert,

JSONObject json = new JSONObject();
json.put("uniqueArrays", new JSONArray(items));
String arrayList = json.toString();

Insert the string into db.

To Read, Read the string from db as String,

 JSONObject json = new JSONObject(stringreadfromsqlite);
  ArrayList items = json.optJSONArray("uniqueArrays");
like image 57
Sukumar Avatar answered Sep 19 '22 13:09

Sukumar