5.3.Example - データベースのリストを得る Get List of Databases
Prev | Chapter5.Example Programs | Next

## 5.3.Example - Get List of Databases

本節の用例は､中央集中処理系に知られているデータベース(それは pg_database表に保存されている)のリストを返す手続き函数を示す｡ The examples in this section show a procedure to return a list of the databases known to the server (which are stored in the `pg_database` table).

註意 接続命令が使用者名や暗証番号を指定しない｡これは､環境変数等で信頼性を提供しない限り､通常は､不都合｡ Note that the connect command does not specify a username or password. This would usually fail, unless the credentials are provided through some other means such as the environment variables.

最初の用例は､pg_exec函数を使う｡ The first example uses the pg_exec function.

Example5.3.Connect and get list of databases - pg_exec

    proc getDBs { {host "localhost"} {port "5432"} } {
        set conn [pg_connect -conninfo "host=$host port=$port dbname=template1"]
        set res [pg_exec $conn "SELECT datname FROM pg_database ORDER BY datname"]
        set datnames {}
        set ntups [pg_result $res -numTuples]
        for {set i 0} {$i < $ntups} {incr i} {
            lappend datnames [lindex [pg_result $res -getTuple $i] 0]
        }
        pg_result $res -clear
        pg_disconnect $conn
        return $datnames
    }

これは､返された行を繰返す為に､同じ手続きをpg_select を使う用例である｡ Here is the same procedure, using pg_select to iterate over the returned rows.

Example5.4.Connect and get list of databases - pg_select

    proc getDBs { {host "localhost"} {port "5432"} } {
        set conn [pg_connect -conninfo "host=$host port=$port dbname=template1"]
        set datnames {}
        pg_select $conn "SELECT datname FROM pg_database ORDER BY datname" data {
            lappend datnames $data(datname)
        }
        pg_disconnect $conn
        return $datnames
    }

Prev | Up | Next
5.2.Example - Connect and query with pg_execute | Home | 5.4.Example - The Different Ways to Get Query Results