nokboo
nokboo

Reputation: 41

Simple bookmark manager in SQLite

I'd like to prepare a simple SQLite database to be used for storing (tagged) Internet bookmarks, since it seems there is no simple tool out there for doing this.

I imagine I can have three tables, one for the URLs, another for the tags, and another one for the relationships between URLs and tags.

For example:

URL:

Tag:

Relationship:

The problem is my knowledge of SQLite is very small, and the only way I found for retrieving all URLs with the "map" tag is via nested selects:

select name from url where id=(
   select url_id from rel where tag_id=(
      select id from tag where name = 'map'
   )
);

Manually storing bookmarks is a boring operation, too.

So, is there some more efficient way of accomplishing this?

Thank you.

Upvotes: 4

Views: 692

Answers (1)

d5e5
d5e5

Reputation: 442

--
-- URLs
--
CREATE TABLE urls (
  id INTEGER PRIMARY KEY,
  url TEXT NOT NULL
);

--
-- Tags
--
CREATE TABLE tags (
  id INTEGER PRIMARY KEY,
  tag TEXT NOT NULL
);

--
-- UrlTags
--
CREATE TABLE UrlTags (
  id INTEGER PRIMARY KEY,
  urls_id INTEGER,
  tags_id INTEGER,
  FOREIGN KEY(urls_id) REFERENCES url(id),
  FOREIGN KEY(tags_id) REFERENCES tags(id)
);

--
-- URLS sample data
--
INSERT INTO urls VALUES (null, 'http://www.google.com/');
INSERT INTO urls VALUES (null, 'http://maps.google.com/');

--
-- Tags sample data
--
INSERT INTO tags VALUES (null, 'google');
INSERT INTO tags VALUES (null, 'map');
INSERT INTO tags VALUES (null, 'search');

--
-- URLtags sample data
-- Let's say http://www.google.com/ has all 3 tags
INSERT INTO urltags VALUES (null, 1, 1);
INSERT INTO urltags VALUES (null, 1, 2);
INSERT INTO urltags VALUES (null, 1, 3);

-- http://maps.google.com/ has only the 'map' tag
INSERT INTO urltags VALUES (null, 2, 2);

--- The following retrieves all urls associated with the 'search' tag
--- which I assume will be only http://www.google.com/
SELECT url
FROM urls u INNER JOIN urltags r ON u.id = r.urls_id
            INNER JOIN tags t ON t.id = r.tags_id
WHERE t.tag = 'search';

Upvotes: 2

Related Questions