Simple Bookmark Manager in SQLite

I would like to prepare a simple SQLite database that will be used to store (tagged) Internet bookmarks, as it seems that there is no simple tool for this.

I assume that I can have three tables, one for URLs, another for tags, and another for the relationship between URLs and tags.

For instance:

URL:

Tag:

  • 1: google
  • 2: map
  • 3: search

Relationship:

  • 1: 1: 1
  • 1: 1: 3
  • 1: 2: 1
  • 1: 2: 2

The problem is that my knowledge of SQLite is very small, and the only way to find all the URLs with the map tag is through the nested select:

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

Manually storing your bookmarks is also a boring operation.

So, is there an even more efficient way to accomplish this?

Thank.

+3
source share
1 answer
--
-- 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 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';
+1
source

All Articles