1

I am trying to store data with the following format, and am not sure of the best way to organize in SQLite:

-Main item

-- Field 1

-- Field 2

-- Field 3 (main image)

---- image path

---- date image was taken

-- Field 4

-- Field 5 (array of images)

---- each image is a path and date pair as above

I was thinking 2 tables? 1 for the main item, with the main image path and date as just two specific columns; then the second table would be the list of images with an id tied to the main item?

Is there a better way to do this? Seems like it would be hassle to manage and update both tables, but if that's the best way then that's how it is

2
  • 1
    The key idea is data normalization, but a full tutorial about that is beyond an answer here. Yes, a good design will require multiple tables but it goes beyond being "how it is", rather it is the preferred, best way to store related data that contains multiple lists and/or "arrays". Commented Nov 12, 2019 at 21:54
  • @CPerkins so the two table approach I highlighted would be best practice here? Since I'm learning I just want to make sure I'm learning best practices vs hack methods Commented Nov 12, 2019 at 21:56

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

5 Comments

Not so regarding two-way foreign keys. You can first turn off foreign key enforcement using PRAGMA foreign_keys = OFF;, define the foreign key constraints on both tables, finally reset the pragma to ON.
Actually, I was also able to define the two-way foreign keys without changing the pragma to OFF. The foreign key constraint is not checked until data is first inserted.
@CPerkins - Thanks for the insight, the answer is updated.
@mattdedek thanks! I will look into relational databases as you suggested and try building off two tables
Glad it works. However, remember that one of the purposes of a foreign key is to constrain values in one table to a set of valid related rows in the other. But the single-column foreign-key reference from mainimage will still allow ANY image.id value, not only the ones that are related to the mainitem row! To fix this, you should create a multi-column foreign key relationship that includes the maintime.id to the image.mainitem column.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.