PostgreSQL Tcl Interface Documentation
Prev | Fast Backward | Chapter 1. pgtcl - Tcl Binding Library | Fast Forward |

# 1.6. Example Program

Example 1-1 shows a small example of how to use the pgtcl commands.

Example 1-1. pgtcl Example Program

    # 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
    proc getDBs { {host "localhost"} {port "5432"} } {
        # datnames is the list to be result
        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
    }

    ## OR an alternative

    proc getDBs { {host "localhost"} {port "5432"} } {
        # datnames is the list to be result
        set conn [pg_connect template1 -host $host -port $port]
        set res [pg_exec $conn "SELECT datname FROM pg_database ORDER BY datname;"]

        set datnames [pg_result $res -list]
        pg_result $res -clear
        pg_disconnect $conn
        return $datnames
    }

    ## OR an alternative

    proc getDBs { {host "localhost"} {port "5432"} } {
        # datnames is the list to be result
        set conn [pg_connect template1 -host $host -port $port]
        set res [$conn "SELECT datname FROM pg_database ORDER BY datname;"]

        set datnames [$res -dict]
        $res -clear
        rename $conn {}
        return [dict get $datnames]
    }

Prev | Home |
Connection/result handles as commands | Up |