summaryrefslogtreecommitdiff
path: root/doc/note/sqlite/sqlite.txt
blob: ddcc0ae14710dc39e9b03d0fe5b2f425bfb9bed9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50


## How to merge two DBs with same schema

BEGIN TRANSACTION;
ATTACH 'gugus.db' AS other;
INSERT INTO "Foo" SELECT * FROM "other"."Foo";
INSERT INTO "Bar" SELECT * FROM "other"."Bar";
COMMIT;
DETACH other;


ATTACH 'bekb-2023.db' AS other;
INSERT INTO "Account" SELECT * FROM "other"."Account";
INSERT INTO "Currency" SELECT * FROM "other"."Currency";
INSERT INTO "AccountType" SELECT * FROM "other"."AccountType";
INSERT INTO "Transaction" SELECT * FROM "other"."Transaction";


## import CSV

csv Example (no headrow):
  1;foo
  2;bar

  sqlite3 foo.db -bail -cmd '.mode csv' -cmd '.separator ;' -cmd '.import foo.csv FooTable'


## Insert conditional

INSERT INTO table(id, text) 
SELECT 5, 'text to insert' 
WHERE NOT EXISTS (SELECT 1 FROM table WHERE id = 5)


## Drop Column

  ALTER TABLE table DROP COLUMN column;


## Add Column

  ALTER TABLE table ADD COLUMN column NOT NULL DEFAULT 'gugus';


## Refs

[drop column TODO vote](https://stackoverflow.com/a/66399224/4415884)
[insert if not exists](https://stackoverflow.com/a/19337206/4415884)