29.4. 例子程序

Example 29-1 演示了一個如何使用這些pgtcl命令的一個小例子.

Example 29-1. pgtcl 例子程序

# getDBs :
#   get the names of all the databases at a given host and port number
#   with the defaults being the localhost and port 5432
#   return them in alphabetical order
#
#  獲取給定主機和端口號上的所有數據庫,缺省是 localhost 和端口 5432
#  並且按照字母順序返回它們
#
proc getDBs { {host "localhost"} {port "5432"} } {
    # datnames 是結果列表
    set conn [pg_connect template1 -host $host -port $port]
    set res [pg_exec $conn "SELECT datname FROM pg_database ORDER BY datname;"]
    set ntups [pg_result $res -numTuples]
    for {set i 0} {$i < $ntups} {incr i} {
	lappend datnames [pg_result $res -getTuple $i]
    }
    pg_result $res -clear
    pg_disconnect $conn
    return $datnames
}