------------------------ Dyalog DataBase Examples ------------------------ ⍝ Table with simple rows ------------------------------------------------------ 'mytab' ddb.create 'name' 80 10 ⍝ simple 10-character rows. 'mytab' ddb.append 'John' ⍝ append a row. 'mytab' ddb.append 2 6⍴'Paul George' ⍝ append more rows. 'mytab' ddb.append 'Ringo' ⍝ append another row. display 'mytab' ddb.get 'name' ⍝ retrieve all 10-char rows. ┌→─────────┐ ↓John │ │Paul │ │George │ │Ringo │ └──────────┘ ddb.remove'mytab' ⍝ remove table. ⍝ Table with nested rows ------------------------------------------------------ 'mytab' ddb.create 'name' 80 ¯10 ⍝ nested ≤10 char rows. 'mytab' ddb.append 'John' ⍝ append a row. 'mytab' ddb.append 'Paul' 'George' 'Ringo' ⍝ append more rows. display 'mytab' ddb.get 'name' ⍝ retrieve all nested rows. ┌→───────────────────────────────┐ │ ┌→───┐ ┌→───┐ ┌→─────┐ ┌→────┐ │ │ │John│ │Paul│ │George│ │Ringo│ │ │ └────┘ └────┘ └──────┘ └─────┘ │ └∊───────────────────────────────┘ ddb.remove'mytab' ⍝ remove table. ⍝ Table with two fields ------------------------------------------------------- 'mytab' ddb.create ('name' 80 ¯10)('age' 83) ⍝ table with two fields. 'mytab' ddb.append 'Mick' 61 ⍝ append a row. 'mytab' ddb.append ('Brian' 'Keith') (62 60) ⍝ append more, 'mytab' ddb.append ('Charlie' 'Bill') (63 67) ⍝ and more rows. display 'mytab' ddb.get 'age' ⍝ retrieve single field. ┌→─────────────┐ │61 62 60 63 67│ └~─────────────┘ display 'mytab' ddb.get 'age' 'name' ⍝ retrieve many fields. ┌→─────────────────────────────────────────────────────────────┐ │ ┌→─────────────┐ ┌→────────────────────────────────────────┐ │ │ │61 62 60 63 67│ │ ┌→───┐ ┌→────┐ ┌→────┐ ┌→──────┐ ┌→───┐ │ │ │ └~─────────────┘ │ │Mick│ │Brian│ │Keith│ │Charlie│ │Bill│ │ │ │ │ └────┘ └─────┘ └─────┘ └───────┘ └────┘ │ │ │ └∊────────────────────────────────────────┘ │ └∊─────────────────────────────────────────────────────────────┘ ⍝ Select field values --------------------------------------------------------- display 'mytab' ddb.get 'age' ⍝ get all age rows. ┌→─────────────┐ │61 62 60 63 67│ └~─────────────┘ display 'mytab' ddb.get (1 0 1 0 1) 'age' ⍝ get alternate rows. ┌→───────┐ │61 60 67│ └~───────┘ 'mytab' ddb.put (0 0 1 0 1) 'age' (61 68) ⍝ update some age values. display 'mytab' ddb.get 'age' ⍝ check update. ┌→─────────────┐ │61 62 61 63 68│ └~─────────────┘ 'mytab' ddb.get (62>'mytab' ddb.get 'age') 'name' ⍝ compound selection. Mick Keith ⍝ Use a _handle_ for better performance --------------------------------------- mytab ← ddb.open 'mytab' ⍝ use handle for speed. mytab ddb.get (62>mytab ddb.get 'age') 'name' ⍝ compound selection. Mick Keith ⍝ Remove table rows ----------------------------------------------------------- mytab ddb.retain 1 0 1 0 1 ⍝ retain alternate rows. display mytab ddb.get 'age' 'name' ⍝ retrieve all fields. ┌→─────────────────────────────────────┐ │ ┌→───────┐ ┌→──────────────────────┐ │ │ │61 60 68│ │ ┌→───┐ ┌→────┐ ┌→───┐ │ │ │ └~───────┘ │ │Mick│ │Keith│ │Bill│ │ │ │ │ └────┘ └─────┘ └────┘ │ │ │ └∊──────────────────────┘ │ └∊─────────────────────────────────────┘ ⍝ Miscellaneous --------------------------------------------------------------- display ddb.defs mytab ⍝ show field definitions. ┌→───────────────────────────────┐ │ ┌→──────────────┐ ┌→─────────┐ │ │ │ ┌→───┐ │ │ ┌→──┐ │ │ │ │ │name│ 80 ¯10 │ │ │age│ 83 │ │ │ │ └────┘ │ │ └───┘ │ │ │ └∊──────────────┘ └∊─────────┘ │ └∊───────────────────────────────┘ display mytab.names ⍝ field names. ┌→─────────────┐ │ ┌→───┐ ┌→──┐ │ │ │name│ │age│ │ │ └────┘ └───┘ │ └∊─────────────┘ ddb.remove mytab ⍝ remove table. ⍝ Finished -------------------------------------------------------------------- See also: →ReadMe← →RefCard← Back to: Contents