36

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. Is it possible? If it is, how to proceed? On the Blackberry platform, I am able to store objects directly in persistent objects. Is it possible to do this with SQLite and Android?

colithium
  • 10,269
  • 5
  • 42
  • 57
Kumar
  • 5,469
  • 20
  • 64
  • 87

3 Answers3

24

You'll need to be able to serialize your object into a byte stream and then recreate your object from a byte stream.

Then, just store that byte stream in your db.

EDIT: Read this article to learn about serialization in Java. It caters to the subject in a much better way than I would be able to by just giving some code snippets.

Lennart
  • 9,657
  • 16
  • 68
  • 84
Prashast
  • 5,645
  • 3
  • 30
  • 31
  • can u pls provide me some code snippet to do serialization of object. – Kumar Aug 11 '09 at 03:55
  • I edited the answer and give a link to an article which teaches you how to do object serialization. – Prashast Aug 11 '09 at 05:28
  • 3
    link leads to Oracle Java start page that has no information about serialization – Budda Oct 28 '12 at 13:59
  • 5
    Link rot. A little more detail would've been better! – Anirudh Ramanathan Oct 28 '12 at 20:20
  • 1
    I was under the impression, that SQLite actually serves the purpose of quickly storing objects in a DB. I've used it for exactly that purpose in other applications. Is it really so, that we can't use SQLite like you'd use it in non-Android apps? Why use SQLite at all on Andorid then, if you have to do SQL Statements anyway? I don't get it. Why did they include SQLite then? – Zelphir Kaltstahl Nov 29 '15 at 18:10
  • The link appears to be dead and no longer go anywhere. Says page not found when visited for me in 2023. – BlueSquare23 Feb 18 '23 at 06:16
6

Instead of storing them in SQLite why not store them in db4o, which is specifically designed for storing objects. A simple tutorial for sotring objects using db4o.

SohailAziz
  • 8,034
  • 6
  • 41
  • 43
  • 6
    DB4O is great in principle. It's super easy to use. BUT unfortunately it has many bugs and development/maintenance seems to have completely stopped. Very sad situation. – Matthias Jan 17 '14 at 13:33
6

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;
}
Praveen
  • 93
  • 1
  • 7