Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving ArrayList in SQLite database in Android

Tags:

I've been working with SQLite on android and I would like to add an arraylist to a column in a table, and then fetch the data back as an arraylist. The arraylist is a list of Longs. I've noticed that SQL has an option for storing BLOBS, however it looks like I need to convert the arraylist to a byte[] first before being able to store it as a blob in my SQLite database.

If anyone has a solution on how to save arraylists into an SQLite database that would be greatly appreciated. Or is there any other option for saving my array of data, i should consider?

like image 532
Mango Avatar asked Jun 29 '10 15:06

Mango


1 Answers

To Insert :

ArrayList<String> inputArray=new ArrayList<String>(); 

//....Add Values to inputArray

Gson gson = new Gson();  String inputString= gson.toJson(inputArray);  System.out.println("inputString= " + inputString); 

use "inputString" to save the value of ArrayList in SQLite Database

To retreive:

Get the String from the SQLiteDatabse what you saved and changed into ArrayList type like below: outputarray is a String which is get from SQLiteDatabase for this example.

Type type = new TypeToken<ArrayList<String>>() {}.getType();  ArrayList<String> finalOutputString = gson.fromJson(outputarray, type); 
  • In SQLite use text as format to store the string Value.....
like image 161
Ashok Avatar answered Oct 23 '22 16:10

Ashok