在 SQLite 数据库中显示给定类别的几个句子

Showing several sentences for a given category in a SQLite database

我正在开发一个应用程序报价,我正在使用本地数据库导入报价。 我这里有一个问题,你可以在图片中看到我想在一个标题中添加多个引号。

例如,当我在应用程序中单击标题 "Life" 时,它只显示一个引号。我想让它显示一个引语,然后我滑到另一个引语。

您需要的是多个 tables,并且要坚持规范化(将数据保持在最低限度),您需要 4 tables。

A table 标题。 A table 用于引号本身。 作者的 table(例如,而不是重复 "Mahatma Ghandi")。 table maps/references 标题和引号(允许多对多关系)。

标题 table需要两列; - 一个是 id(注意 _id 已被使用,因为这可能需要作为 Android 的列名)。 - 一个标题本身。

quote table 需要三栏; - 一个是 id(又是_id)。 - 一个用于报价本身。 - 一个参考作者table的id

作者 table 需要两列; - 一个是 id(又是_id)。 - 一份给作者。

最后一个table(title_quote_reference)需要两列 - 一个参考标题 - 一个参考报价

以下内容可用于构建上述 tables,填充它们和 运行 显示完整内容的基本查询(就用户友好数据而言(即 id 's/references 对用户来说意味着 nothing/little) :-

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
;

运行 上面的 would/should 导致消息如下:-

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

查询结果为:-

如您所见,title 可以有多个引号,quote 也可以有多个标题。

您可以将查询修改为:-

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
;

这将 select 随机引用,例如例如每日报价。

  • 注意上面包括外键,在使用 Android 的原生 SQLite using/executing SQL [=70= 时必须打开这些](在 onOpen 或 onConfigure 方法中)。没有实际需要 REFERENCES ...,因此如果您不想打开外键,可以删除或忽略这些。