Create database named sharks, if file sharks.db exists, SQLite open a connection to it; if not, create a new database
1
sqlite3 sharks.db
Create SQLite table
1
CREATE TABLE sharks(id integer NOT NULL, name text NOT NULL, sharktype text NOT NULL, length integer NOT NULL);
Inserting values
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);
Reading tables
1 2 3
SELECT * FROM sharks; -- View entry based on id we choose SELECT * FROM sharks WHERE id IS 1;
Comment in SQLite
1
-- This is a comment
Update tables in SQLite
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;
Deleting information in SQLite
1
DELETE FROM sharks WHERE age <= 200;
Joining information in SQLite
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