Is it possible to store user defined objects in a SQLite database from Android? For example: I am creating one class and I want to store that class object in the database.
Use this code to convert your object to byte array and store it in your database as "BLOB"
public byte[] makebyte(Dataobject modeldata) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(modeldata);
byte[] employeeAsBytes = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(employeeAsBytes);
return employeeAsBytes;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Use this code to convert your byte array to your object again
public Dataobject read(byte[] data) {
try {
ByteArrayInputStream baip = new ByteArrayInputStream(data);
ObjectInputStream ois = new ObjectInputStream(baip);
Dataobject dataobj = (Dataobject ) ois.readObject();
return dataobj ;
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}