Simple database: Difference between revisions

Line 2,031:
Namespace(_date='2012-08-18T06:03:34.319799', description='Book', field=[['title', 'Splat it'], ['type', 'hardback'], ['special', 'first edition']], tag='PREMIUM')
paddy3118:~$ </pre>
 
=={{header|Racket}}==
<lang racket>
#!/usr/bin/env racket
#lang racket
 
(define (*write file data) ; write data in human readable format (sexpr/line)
(with-output-to-file file #:exists 'replace
(lambda () (for ([x data]) (printf "~s\n" x)))))
(define *read file->list) ; read our "human readable format"
 
(command-line
#:once-any
[("-a") file title category date "Add an entry"
(*write file `(,@(*read file) (,title ,category ,date)))]
[("-d") file title "Delete an entry (all matching)"
(*write file (filter-not (lambda (x) (equal? (car x) title)) (*read file)))]
[("-p") file mode "Print entries, mode = latest, latest/cat, all, by-date"
(define data (*read file))
(define (show item)
(match item [(list title cat date) (printf "[~a] ~a; ~a\n" cat title date)]))
(case (string->symbol mode)
[(all) (for-each show data)]
[(by-date) (for-each show (sort data string<? #:key cadr))]
[(latest) (show (last data))]
[(latest/cat)
(define (last/cat c) (for/last ([x data] #:when (equal? c (cadr x))) x))
(for-each (compose1 show last/cat) (remove-duplicates (map cadr data)))]
[else (error 'sdb "bad printout mode")])])
</lang>
 
Sample run:
<pre>
$ ./sdb -h
sdb [ <option> ... ]
where <option> is one of
/ -a <file> <title> <category> <date> : Add an entry
| -d <file> <title> : Delete an entry (all matching)
\ -p <file> <mode> : Print entries, mode = latest, latest/cat, all
--help, -h : Show this help
-- : Do not treat any remaining argument as a switch (at this level)
/|\ Brackets indicate mutually exclusive options.
Multiple single-letter switches can be combined after one `-'; for
example: `-h-' is the same as `-h --'
$ ./sdb -a Stuffs A-Book books 2013-01-01
$ ./sdb -a Stuffs Another-Book books 2013-01-02
$ ./sdb -a Stuffs Some-CD cds 2013-01-03
$ ./sdb -a Stuffs A-Bad-CD cds 2013-01-04
$ ./sdb -p Stuffs all
[books] A-Book; 2013-01-01
[books] Another-Book; 2013-01-02
[cds] Some-CD; 2013-01-03
[cds] A-Bad-CD; 2013-01-04
$ ./sdb -p Stuffs latest
[cds] A-Bad-CD; 2013-01-04
$ ./sdb -p Stuffs latest/cat
[books] Another-Book; 2013-01-02
[cds] A-Bad-CD; 2013-01-04
$ ./sdb -d Stuffs A-Bad-CD
$ ./sdb -p Stuffs by-date
[books] A-Book; 2013-01-01
[books] Another-Book; 2013-01-02
[cds] Some-CD; 2013-01-03
</pre>
 
=={{header|Ruby}}==
Line 2,296 ⟶ 2,360:
{"fields":["event_title","start_time","stop_time","location","event_description"],"items":{"6dd02195-1efe-40d1-b43e-c2efd852cd1d":{"event_title":"Wife's Birthday","start_time":"2011-11-01","stop_time":"2011-11-01","location":"","event_description":"happy 39th"},"0190b835-401d-42da-9ed3-1d335d27b83c":{"event_title":"Parent-Teacher Conference","start_time":"2011-11-03 19:30","stop_time":"2011-11-03 20:00","location":"school","event_description":"desc"},"4023e6f1-bcc1-49e5-a59f-138157b413f4":{"event_title":"Buy gift for wife","start_time":"2011-10-31 16:00","stop_time":"2011-10-31 16:30","location":"the mall","event_description":"hmm, maybe jewelery?"}},"history":[[1320349951.000625,"6dd02195-1efe-40d1-b43e-c2efd852cd1d"],[1320350045.4736252,"0190b835-401d-42da-9ed3-1d335d27b83c"],[1320350102.9486248,"4023e6f1-bcc1-49e5-a59f-138157b413f4"]],"tags":{"birthday":["6dd02195-1efe-40d1-b43e-c2efd852cd1d"],"family":["6dd02195-1efe-40d1-b43e-c2efd852cd1d","0190b835-401d-42da-9ed3-1d335d27b83c","4023e6f1-bcc1-49e5-a59f-138157b413f4"],"school":["0190b835-401d-42da-9ed3-1d335d27b83c"],"last-minute":["4023e6f1-bcc1-49e5-a59f-138157b413f4"]}}
</pre>
 
=={{header|Run BASIC}}==
<lang runbasic>sqliteconnect #sql, "f:\client.db" ' Connect to the DB