views:

304

answers:

2

Hello:

I am building a Tcl application that reads off of a Sqlite Db. Currently, I can enter data into the database using the Tcl frontend. Now, I am trying to figure out how to display the data within the Sqlite Db from the Tcl frontend.

After a little bit of research, I found that the treeview widget would work well for my needs. I now have the following code:

set z1 [ttk::treeview .c1.t1 -columns {1 2} -show headings] $z1 heading #1 -text "First Name" $z1 heading #2 -text "Last Name"

proc Srch {} {global z1 sqlite3 db test.db pack $z1 db close }

When the "Srch" procedure is executed (button event), the treeview (z1) appears with the headings First Name and Last Name. Additionally, the Sqlite Db gets connected, then closes.

I wanted to add code that would populate the treeview from the Sqlite Db between connecting to the Db and packing the treeview (z1). Does anyone know the correct syntax to populate a Tcl treeview with data from Sqlite?

Thank you everyone in advance,

DFM

+1  A: 

After a few hours of trial and error, I finally figured out how to populate a treeview widget with sqlite data. Unfortunately, I could not find any web resources so in case anyone else has this problem, the answer is as follows:

set z1 [ttk::treeview .c1.t1 -columns {first last} -show headings]

proc Srch {} {
    global z1
    sqlite3 db test.db
    pack $z1
    set data [db eval {SELECT * FROM t1}]
    foreach col {first last} name {First Last} {
        .c1.t1 heading $col -text $name
    }
    foreach {first last} $data {
        .c1.t1 insert {} end -values [list $first $last]
    }
    db close
}

Basically, z1 is multi-column list (tree) and Srch is the button driven procedure. When executed the tree gets populated with first and last names, corresponding to each column.

DFM
+1  A: 

Better to do this:

proc Srch {} {
    global z1
    pack z1
    foreach col {first last} name {First Last} {
        $z1 heading $col -text $name
    }
    sqlite3 db test.db
    db eval {SELECT first,last FROM t1} {
        $z1 insert {} end -values [list $first $last]
    }
    db close
}

This uses the fact that SQLite integrates very nicely with Tcl indeed.

Donal Fellows