我正在开发应用程序报价,并且正在使用本地数据库导入报价。我有一个问题,正如您在图片中看到的那样,我想在一个标题中添加多个引号。
例如,当我在应用程序中单击标题“生活”时,它只显示一个引用。我希望它显示一个报价,然后我滑到另一个报价。
我正在开发应用程序报价,并且正在使用本地数据库导入报价。我有一个问题,正如您在图片中看到的那样,我想在一个标题中添加多个引号。
例如,当我在应用程序中单击标题“生活”时,它只显示一个引用。我希望它显示一个报价,然后我滑到另一个报价。
您需要的是多个表,并且要遵守规范化(将数据保持在最低限度),您需要 4 个表。
标题表。报价表本身。作者的表格(例如,而不是重复“圣雄甘地”)。映射/引用标题和引号的表(允许多对多关系)。
标题表需要两列;- 一个用于它的id(注意_id已被使用,因为这可能需要作为 Android 的列名)。- 一个用于标题本身。
报价表需要三列;- 一个代表它的id(又是 _id)。- 一个用于报价本身。- 一个用于引用作者表的id
作者表需要两列;- 一个代表它的id(又是 _id)。- 一个给作者。
最后一个表 ( title_quote_reference ) 需要两列 - 一列引用标题 - 一列引用引用
以下内容可用于构建上述表格,填充它们并运行显示全部内容的基本查询(就用户友好数据而言(即 id/references 对用户来说没有任何意义/意义不大):-
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
;
运行上述将/应该导致以下消息:-
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
查询的结果将是:-
如您所见,一个标题可以有多个引号,一个引号也可以有多个标题。
您可以将查询修改为:-
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
;
然后,这将选择一个随机报价,例如当日报价。
REFERENCES ...
因此如果您不想打开外键,可以删除或忽略这些。