Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store object in sqlite database?

Tags:

android

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?

like image 222
Kumar Avatar asked Aug 07 '09 06:08

Kumar


People also ask

Can we store object in SQLite?

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.

How do I store Python objects in SQLite?

One option is to use an O/R mapper like SQLObject. It will do most of the plumbing to persist the Python object to a database, and it supports SQLite.

How is data stored in SQLite database?

The Android SDK provides dedicated APIs that allow developers to use SQLite databases in their applications. The SQLite files are generally stored on the internal storage under /data/data/<packageName>/databases. However, there are no restrictions on creating databases elsewhere.


2 Answers

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.

like image 114
Prashast Avatar answered Oct 14 '22 16:10

Prashast


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.

like image 34
SohailAziz Avatar answered Oct 14 '22 15:10

SohailAziz