How to Store Object in Sqlite Database

how to store custom object with list in sqlite database in android

While indeed you can not store objects in sqlite, you can serialize it and store the serialized object:

Gson gson = new Gson();
String toStoreObject = gson.toJson(modelObject, Model.class);

And when you get the string back just do:

Model modelObject = gson.fromJson(storedObjectString, Model.class);

Or just serialize the property of the object (in your case the List).

Different things I would do:

  1. Create another table for the images and link that table to the table in which you store your Model object (n to 1 relationship)

  2. Don't store images in sqlite. Store them in the internal storage and in sqlite save just the path to the file.

  3. If you really really want to store the images in sqlite, convert the image to a base64 string and store that one.

New to SQLite - how to store data which is a list of object with sub objects and array?

As noted in the comments, you should read up on relational database basics. The vocabulary for databases is different from programming. SQLite is a basic database engine and only supports basic data types, so after learning relational database concepts, you should read up on sqlite specifically.

Here's an informal summary for your problem:

  1. Your data model requires two tables as you noted.
  2. What you call 'Fields' are 'columns' in the tables. Columns have a type such as INTEGER or TEXT (https://www.sqlite.org/datatype3.html).
  3. Arrays are expressed with FOREIGN KEYs (https://sqlite.org/foreignkeys.html). A foreign key is a property that references a record in another column by matching a unique value in a specific column of the other table. You build an array of values by selecting records from the table with the foreign key that match the object you care about.
  4. Note that SQL code in an app generally isn't required. Try looking for a framework or Object Relational Mapper (ORM) that takes care of mapping database tables to objects, columns to properties and handles the persistence and schema updates for you. I can say from experience, manual SQL code is difficult to maintain and seriously complicates testing.

Your specific problem could be solved with this schema:

CREATE TABLE mainitem(
id INTEGER PRIMARY KEY,
field1 TEXT,
field2 INTEGER
);

CREATE TABLE image(
id INTEGER PRIMARY KEY,
datetaken TEXT,
path TEXT,
mainitem INTEGER,
FOREIGN KEY(mainitem) REFERENCES mainitem(id)
);

ALTER TABLE mainitem
ADD COLUMN mainimage INTEGER REFERENCES mainitem(id) DEFAULT NULL;

To get your array of images, where the id of your mainitem == 3:

SELECT * FROM image WHERE mainitem = 3;

To get your main item with its main image:

SELECT * 
FROM mainitem
LEFT OUTER JOIN image
ON mainitem.mainimage = image.id
WHERE mainitem.id = 3;

How to store employee object in SQLite Database in android

you can use the json of your objects. it is so easy and you can use liberaries such as gson

How to store objects in sqlite table in react-native?

You did water_characterstics.toString() which always returns [object Object]. Instead, you should do JSON.stringify(water_characterstics).

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

Storing Objects in SQLite. Is It Possible?

My guess is that it could be something like dumping binary data -an struct for i.e.- and the answer is yes (blob datatype).

Anyway, if I were doing something like that, I'd store encoded json in a text db field, for the sake of simplicity and portability. Keep in mind that this choice depends on many facts, for example how often the db will be accessed to retrieve/save these objects, or how big are the objects going to be, etc etc.

Hope this helps



Related Topics



Leave a reply



Submit