What you need is multiple tables and to adhere to normalisation (keeping data to a minimum) you'd want 4 tables.
A table for the titles.
A table for the quotes themselves.
A table for the authors (e.g rather than repeating "Mahatma Ghandi").
A table that maps/references titles and quotes (allowing a many to many relationship).
The title table would need two columns;
- one for it's id (note _id has been used as this is potentially required as a column name for Android).
- one for the title itself.
The quote table would need three columns;
- one for it's id (_id again).
- one for the quote itself.
- one for the reference to the author table's id
The author table would need two columns;
- one for it's id (_id again).
- one for the author.
The last table (title_quote_reference) needs two columns
- one to reference the title
- one to reference the quote
The following could be used to build the above tables, populate them and run a basic query that display the full contents (as far as user friendly data is concerned (i.e. the id's/references would mean nothing/little to a user) :-
DROP TABLE IF EXISTS title_quote_map;
DROP TABLE IF EXISTS quote;
DROP TABLE IF EXISTS author;
DROP TABLE IF EXISTS title;
CREATE TABLE IF NOT EXISTS author (_id INTEGER PRIMARY KEY, author TEXT UNIQUE);
CREATE TABLE IF NOT EXISTS title (_id INTEGER PRIMARY KEY, title TEXT UNIQUE);
CREATE TABLE IF NOT EXISTS quote (_id INTEGER PRIMARY KEY, quote TEXT UNIQUE, author_reference INTEGER REFERENCES author(_id));
CREATE TABLE IF NOT EXISTS title_quote_map (
title_reference INTEGER REFERENCES title(_id),
quote_reference INTEGER REFERENCES quote(_id),
UNIQUE (title_reference, quote_reference));
INSERT INTO title (title) VALUES
('Life'),
('Happiness'),
('Positivity'),
('Famous Quotes'),
('Friendship'),
('Love'),
('Family'),
('Motivation')
;
INSERT INTO author (author) VALUES
('Leonardo da-Vinci'),
('Mahatma Ghandi'),
('Winston Churchill'),
('anon')
;
INSERT INTO quote (quote,author_reference) VALUES
('Life is my message.',2),
('Not how long, but how well you have lived.',4),
('Never in the field of human combat have so many owed so much to so few',3),
('I love those who can smile in trouble',1)
;
INSERT INTO title_quote_map VALUES
(1,1),(1,2),(1,4), -- Life quotes
(2,2),(2,4), -- Happiness quotes
(3,1),(3,2),(3,4), -- Positivity quotes
(4,1),(4,3),(4,4), -- Famous quotes
(6,4), -- Love quotes
(8,1),(8,2),(8,4)
;
SELECT quote.quote, title.title, author.author
FROM title_quote_map
JOIN title ON title._id = title_quote_map.title_reference
JOIN quote ON quote._id = title_quote_map.quote_reference
JOIN author ON author._id = quote.author_reference
;
Running the above would/should result in messages along the lines of :-
DROP TABLE IF EXISTS title_quote_map
> OK
> Time: 0.622s
DROP TABLE IF EXISTS quote
> OK
> Time: 0.358s
DROP TABLE IF EXISTS author
> OK
> Time: 0.359s
DROP TABLE IF EXISTS title
> OK
> Time: 0.282s
CREATE TABLE IF NOT EXISTS author (_id INTEGER PRIMARY KEY, author TEXT UNIQUE)
> OK
> Time: 0.242s
CREATE TABLE IF NOT EXISTS title (_id INTEGER PRIMARY KEY, title TEXT UNIQUE)
> OK
> Time: 0.307s
CREATE TABLE IF NOT EXISTS quote (_id INTEGER PRIMARY KEY, quote TEXT UNIQUE, author_reference INTEGER REFERENCES author(_id))
> OK
> Time: 0.642s
CREATE TABLE IF NOT EXISTS title_quote_map (
title_reference INTEGER REFERENCES title(_id),
quote_reference INTEGER REFERENCES quote(_id),
UNIQUE (title_reference, quote_reference))
> OK
> Time: 0.307s
INSERT INTO title (title) VALUES
('Life'),
('Happiness'),
('Positivity'),
('Famous Quotes'),
('Friendship'),
('Love'),
('Family'),
('Motivation')
> Affected rows: 8
> Time: 0.34s
INSERT INTO author (author) VALUES
('Leonardo da-Vinci'),
('Mahatma Ghandi'),
('Winston Churchill'),
('anon')
> Affected rows: 4
> Time: 0.276s
INSERT INTO quote (quote,author_reference) VALUES
('Life is my message.',2),
('Not how long, but how well you have lived.',4),
('Never in the field of human combat have so many owed so much to so few',3),
('I love those who can smile in trouble',1)
> Affected rows: 4
> Time: 0.24s
INSERT INTO title_quote_map VALUES
(1,1),(1,2),(1,4), -- Life quotes
(2,2),(2,4), -- Happiness quotes
(3,1),(3,2),(3,4), -- Postivity quotes
(4,1),(4,3),(4,4), -- Famout quotes
(6,4), -- Love quotes
(8,1),(8,2),(8,4)
> Affected rows: 15
> Time: 0.242s
SELECT quote.quote, title.title, author.author
FROM title_quote_map
JOIN title ON title._id = title_quote_map.title_reference
JOIN quote ON quote._id = title_quote_map.quote_reference
JOIN author ON author._id = quote.author_reference
> OK
> Time: 0s
The result from the query would be :-

As you can see a title can have multiple quotes and a quote can also have multiple titles.
You could modify the query to be :-
SELECT quote.quote, title.title, author.author
FROM title_quote_map
JOIN title ON title._id = title_quote_map.title_reference
JOIN quote ON quote._id = title_quote_map.quote_reference
JOIN author ON author._id = quote.author_reference
ORDER BY random()
LIMIT 1
;
This would then select a random quote e.g. for quote of the day, as an example.
- Note the above includes Foreign Keys, these have to be turned on when utilising the Android's native SQLite using/executing the SQL "PRAGMA foreign_keys=ON;" (in the onOpen or the onConfigure method). There is no actual need to have the
REFERENCES ...
so these could be removed or ignored if you didn't want to turn Foreign Keys on.