Table of Contents
- 1. Sqlite3 Commands
- 2. Sqlite3 date and time
- 3. Sqlite3 timestamp, Sqlite3 epoach time, source file
- 4. Sqlite3: Create Table, Insert data to table
- 5. Creat Table with epoch time
- 6. Source file from command line
- 7. Copy data from one table to other table
- 8.
CodeBlock_copy
has different column from CodeBlock - 9. Use the following command to copy data
CodeBlock_copy
=> CodeBlock - 10. Sqlite3 Haskell delete row from a table
- 11. Sqlite3: Foreign Key
- 12. Sqlite3: INNER JOIN
- 13. Sqlite3: Check Whether column is NULL or NOT
- 14. Sqlite3 bookmark
- 15. Sqlite3: If and Else inside query
- 16. Sqlite3: How to use GROUP BY in table
- 17. Sqlite3: SELECT DISTINCT Column
- 18. Sqlite3: datatype
1 Sqlite3 Commands
> .table # Show all tables > .schema # Show table structure > .read # Read myscript.sql > .output /tmp/output.x # Redir stdout to file > .output # Switch back to stdout
2 Sqlite3 date and time
/Users/aaa/myfile/bitbucket/database/epochtime.sql
3 Sqlite3 timestamp, Sqlite3 epoach time, source file
-- Save file to /tmp/test.sql -- source db.sqlite -- sqlite3 trydb.db < /tmp/test.sql -- epoachtime CREATE TABLE tb1 ( entry_id INTEGER PRIMARY KEY AUTOINCREMENT, create_date TEXT DEFAULT (datetime()), edit_date TEXT, addedtime DATETIME DEFAULT CURRENT_TIMESTAMP, epochtime DATETIME DEFAULT (strftime('%s', 'now')), entry_text TEXT NOT NULL ); insert into tb1 (edit_date, entry_text) VALUES('333', 'entry_text')
4 Sqlite3: Create Table, Insert data to table
- IF and ELSE, CASE WHEN expr THEN expr ELSE expr END
-- Sun 5 Mar 23:35:34 2023 -- Sqlite3 -- /Users/aaa/myfile/bitbucket/database/sqlite3/test_table1.sql -- DROP TABLE test_table1; CREATE TABLE test_table1( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, address TEXT, age INTEGER, addedtime DATETIME DEFAULT (strftime('%s', 'now')), score INTEGER DEFAULT 0 ); INSERT INTO test_table1(name, address, age) VALUES('David', '100 King Street', 18); INSERT INTO test_table1(name, address, age) VALUES('King', '200 Queen Street', 28); INSERT INTO test_table1(name, address, age) VALUES('Queen', NULL, 38); INSERT INTO test_table1(name, address, age) VALUES('Jack', '300 Jack Street', NULL); -- SELECT * FROM test_table1; SELECT name, CASE WHEN address IS NULL THEN 'Fake address' ELSE address END, CASE WHEN age IS NULL THEN 100 ELSE age END FROM test_table1;
5 Creat Table with epoch time
CREATE TABLE CodeBlock ( id INTEGER PRIMARY KEY AUTOINCREMENT, header TEXT, codeblock TEXT, addedtime DATETIME DEFAULT (stftime('%s', 'now')), score INTEGER DEFAULT 0 ); -- 1 | a:*.sql:c\nine1 line2|a:*.sql:c\nline 22 | 1720133679 | 0 | v strftime('%s', 'now') -- The following DOES NOT WORK -- addedtime DATETIME DEFAULT stftime('%s', 'now'), -- Need brackets (..)
6 Source file from command line
-- $b/database/epochtime.sql sqlite3 trdb.db < epochtime.sql sqlite3 trdb.db select * from tb1;
7 Copy data from one table to other table
-- We have two tables with different columns: CodeBlock and oldtable -- and we want to copy data from CodeBlock_copy to CodeBlock -- CodeBlock table CREATE TABLE CodeBlock ( id INTEGER PRIMARY KEY AUTOINCREMENT, header TEXT, codeblock TEXT, addedtime DATETIME DEFAULT (strftime('%s', 'now')), score INTEGER DEFAULT 0 );
8 CodeBlock_copy
has different column from CodeBlock
CREATE TABLE CodeBlock_copy ( id INTEGER PRIMARY KEY AUTOINCREMENT, header TEXT, codeblock TEXT );
9 Use the following command to copy data CodeBlock_copy
=> CodeBlock
INSERT INTO CodeBlock (header, codeblock) SELECT header, codeblock FROM CodeBlock_copy
10 Sqlite3 Haskell delete row from a table
-- id is variable let pidInt = fromIntegral pid execute conn Query{fromQuery = s2Text "DELETE FROM CodeBlock WHERE id = ? "} (Only (pidInt::Int64)) deleteURLHas::String -> String -> IO () deleteURLHas s dbFile = do conn <- open dbFile let sql_delete = Query {fromQuery = toSText qStr} execute conn sql_delete () -- execute_ conn sql_delete return () where -- qStr = "DELETE FROM urls WHERE id IN (SELECT id FROM urls U WHERE U.id IN (SELECT urlId FROM items X WHERE title LIKE '%Vansky%'));" s1 = "DELETE FROM urls WHERE id IN (SELECT id FROM urls U WHERE U.id IN (SELECT urlId FROM items X WHERE title LIKE '%" s2 = "%'));" qStr = s1 ++ s ++ s2
11 Sqlite3: Foreign Key
Monday, 06 March 2023 12:47 PST Current Sqlite3 Version is 3.32.3 which does not support foreign key contains. Sqlite3 3.6.19 supports foreign key contains ? I can not intall on my macOS.
DROP TABLE post; create table post( id INTEGER PRIMARY KEY AUTOINCREMENT, description TEXT ); INSERT INTO post (description) values('Awesome Iphone'); INSERT INTO post (description) values('Awesome Samsung'); DROP TABLE image; create table image( id INTEGER PRIMARY KEY AUTOINCREMENT, path TEXT, post_id INTEGER REFERENCES post(id) ); INSERT INTO image (path, post_id) values('/tmp/img1.png', 1); INSERT INTO image (path, post_id) values('/tmp/img2.png', 10); INSERT INTO image (path, post_id) values('/tmp/img3.png', 10);
12 Sqlite3: INNER JOIN
CREATE TABLE urls( id INTEGER PRIMARY KEY, url TEXT NOT NULL ); CREATE TABLE items( id INTEGER PRIMARY KEY, title TEXT, urlId INTEGER REFERENCES urls(id) ON DELETE SET NULL ); -- INNER JOIN SELECT U.id, U.url, I.title FROM urls U INNER JOIN items I ON I.urlId = U.id WHERE I.title LIKE '%Google%'; ↑ ↑ | | | + → WHERE cause for the new intersected table | + → INNER JOIN condition + → Intersection of two tables with condition I.urlId = U.id
13 Sqlite3: Check Whether column is NULL or NOT
14 Sqlite3 bookmark
SELECT id, title from moz_bookmarks where title IS NULL;
15 Sqlite3: If and Else inside query
16 Sqlite3: How to use GROUP BY in table
- SEE: /Users/aaa/myfile/bitbucket/database/createteststudent.sql
- Create table
DROP TABLE test_student; CREATE TABLE test_student( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, age INTEGER NOT NULL ); INSERT INTO test_student(name, age) VALUES('David', 20); INSERT INTO test_student(name, age) VALUES('Mike', 30); INSERT INTO test_student(name, age) VALUES('Smith', 20); INSERT INTO test_student(name, age) VALUES('John', 40); INSERT INTO test_student(name, age) VALUES('William', 20);
- GROUP BY
SELECT count(id) FROM test_student GROUP BY age
17 Sqlite3: SELECT DISTINCT Column
Select distinct rows from table
SELECT DISTINCT age from test_student
18 Sqlite3: datatype
- Sqlite3 supports the following types
- NULL the value is null value.
- INTEGER the value is signed integer, 0, -1, 3 or 8 bytes dependent on the magnitude of the value.
- REAL the value is floating value, stored as IEEE floating point number.
- TEXT the value is text string, stored as database encoding (UTF-8, UTF-16BE or UTF-16LE).
- BLOB the value is a blob of data, stored exactly as it was input.