On 2/9/12, rod <[email protected]> wrote:
> My apologies if this question should be directed to some other list.
>
> I'm looking for a better way to printout the:
> Column Headers,
> followed by the table contents -
> in comma separated value (tsv) format
> from a SELECT statement:
>
>
> So far I have two solutions, neither seems elegant enough.
>
> The second solution makes use of a test of count
I'm excited, I may have solved my problem. By setting a trace I am
able to reset a variable called counter to -0- every time a new sql
statment is compiled. In addition I also save the sql statement to
be evaluated to a variable called ::last_sql via:
# this trace is executed at the end of the compiling of an sql statement
sql trace {set ::count 0; set ::last_sql }
Then this proc will print the headers if ::count=0 (ie only prior to
processing the the first row of the select results.
proc fqry {row {sep "\t"} {header 1}} {
# you can't pass the value in the array to the proc
# you need to upvar it to another var
upvar $row g_row
set val {}
set col {}
set col_name {}
foreach {col_name } $g_row(*) {
lappend val $g_row($col_name)
lappend col $col_name
set len [string length $col_name]
set dash [string repeat - $len]
lappend hd $dash
}
#always prints the vals; only print headers and "-------"'s if counter==0
if {$::count==0 && $header } {puts [join $col $sep]; puts $hd}
puts [join $val $sep]
incr ::count
}
Usage:
%sql eval "Select col2, col4, col6 as Results from table 1" q_array
{fqry q_array}
Q) Does this seem like the way to accomplish this??? Are there other
typical uses of trace that I should be aware of prior to expanding on
this solution.
_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users