Re: FOSS equivalents of *OLD* database and spreadsheet tools?
On Mon, Jul 27, 2020 at 11:16:45AM -0400, Michael Stone wrote:
> On Mon, Jul 27, 2020 at 08:09:36AM -0400, Greg Wooledge wrote:
> > For a project of this size and scope, a Tcl application with an sqlite3
> > database in a local file seems well suited.
>
> Only on the internet can someone ask a simple question and get tcl as the
> answer. :-/
OK, here's a quick program to show how it might be done. The interface
is quite primitive, but it's a proof of concept.
You need to install the libsqlite3-tcl package, if you're using Debian's
version of Tcl. If you compile Tcl from upstream source, this package
is included by default.
Here's the program:
=========================================================
#!/usr/bin/tclsh8.6
package require sqlite3
set dbfile ./food.db
proc usage {} {
global argv0
puts stderr "usage: $argv0 {add|print|dump} arguments"
exit 1
}
if {[file exists $dbfile]} {
sqlite3 db $dbfile
} else {
sqlite3 db $dbfile
db eval {create table food (name text, calories real)}
}
lassign $argv cmd food cals
switch -- $cmd {
add {
if {[llength $argv] != 3} {usage}
db eval {insert into food(name, calories) values (:food, :cals)}
}
print {
if {[llength $argv] != 2} {usage}
db eval {select calories from food where name = :food} v {}
if {! [info exists v(calories)]} {
puts stderr "Food '$food' not found"
exit 1
}
puts $v(calories)
}
dump {
db eval {select name, calories from food order by name} v {
puts [format "%-30.30s %f" $v(name) $v(calories)]
}
}
default {usage}
}
=========================================================
And running it:
unicorn:~$ ./foo
usage: ./foo {add|print|dump} arguments
unicorn:~$ ./foo add pretzels 50
unicorn:~$ ./foo add corn 30
unicorn:~$ ./foo print corn
30.0
unicorn:~$ ./foo print 'potato chips'
Food 'potato chips' not found
unicorn:~$ ./foo dump
corn 30.000000
pretzels 50.000000
unicorn:~$ ls -l food.db
-rw-r--r-- 1 greg greg 8192 Jul 27 11:31 food.db
unicorn:~$ ./foo add corn 35
unicorn:~$ ./foo dump
corn 30.000000
corn 35.000000
pretzels 50.000000
Oops. Looks like we should consider making the name a unique index.
Well, you can add that to your version.
Do you consider this "difficult"? If so, you are probably approaching
this problem as a non-programmer, in which case I don't know what to
tell you. Programming languages exist for a reason, and Tcl is one of
the easiest ones for this particular job.
Reply to: