February 4, 2023

Install and Use SQLite on Ubuntu 20.04

Steps

Install SQLite

  1. (Optional) Update your package list
1
sudo apt update
  1. Install SQLite
1
sudo apt install sqlite3
  1. Verify installation
1
sqlite3 --version

Use SQLite

1
sqlite3 sharks.db
1
CREATE TABLE sharks(id integer NOT NULL, name text NOT NULL, sharktype text NOT NULL, length integer NOT NULL);
1
2
3
INSERT INTO sharks VALUES (1, "Sammy", "Greenland Shark", 427);
INSERT INTO sharks VALUES (2, "Alyoshka", "Great White Shark", 600);
INSERT INTO sharks VALUES (3, "Himari", "Megaladon", 1800);
1
2
3
SELECT * FROM sharks;
-- View entry based on id we choose
SELECT * FROM sharks WHERE id IS 1;
1
-- This is a comment
1
2
3
4
5
6
-- Add columns
ALTER TABLE sharks ADD COLUMN age integer;
-- Update values
UPDATE sharks SET age = 272 WHERE id=1;
UPDATE sharks SET age = 70 WHERE id=2;
UPDATE sharks SET age = 40 WHERE id=3;
1
DELETE FROM sharks WHERE age <= 200;
1
2
3
4
5
6
7
8
9
10
11
12
-- Create table and insert value
CREATE TABLE endangered (id integer NOT NULL, status text NOT NULL);
INSERT INTO endangered VALUES (1, "near threatened");
-- Join tables
SELECT * FROM sharks INNER JOIN endangered on sharks.id = endangered.id;
-- Output
1|Sammy|Greenland Shark|427|272|1|near threatened

-- Specify desired output
SELECT sharks.id, sharks.name, sharks.sharktype, sharks.length, sharks.age, endangered.status FROM sharks INNER JOIN endangered on sharks.id = endangered.id;
-- Output
1|Sammy|Greenland Shark|427|272|near threatened

Typing your system End-Of-File character (usually Control-D). Use the interrupt character (usually a Control-C) to stop a long-running SQL statement.



Reference

About this Post

This post is written by Andy, licensed under CC BY-NC 4.0.

#SQLite#Ubuntu