1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
# Load the sql package:
package require sql
# Connect to the database
set conn [sql connect]
# select the database 'test' to be used.
set res [catch {sql selectdb $conn test} msg]
if {$res != 0} {
puts "Sorry, could not select the database 'test': $msg"
}
# Create a table and put some data in it:
catch {sql exec $conn "drop table junk"}
set res [catch {
sql exec $conn "create table junk (i integer, r real, s char(10))"
} msg]
if {$res != 0} {
puts "Unable to create table junk: $msg"
}
# Put some dummy data in:
for {set i 0} {$i < 10} {incr i} {
sql exec $conn "insert into junk values ($i, $i.01, 'xx $i xx')"
}
# Do a select and display the results
sql query $conn "select * from junk where i > 3"
while {[set row [sql fetchrow $conn]] != ""} {
set i [lindex $row 0]
set r [lindex $row 1]
set s [lindex $row 2]
puts "$i\t$r\t$s"
}
sql endquery $conn
sql disconnect $conn
|