Chapter 5. Example Programs

Abstract

This chapter contains several examples of pgtcl. Most of these are not self-contained programs, and in many cases error checking has been omitted for clarity.

5.1. Example - Connect and query with pg_exec

This example shows how to connect to the database and get the server version string using pg_exec.

Note that pg_result ... -getTuple always returns a Tcl list of values, even if only a single column of data is produced by the query. To get the first (or only) column value, you must use lindex.

Example 5.1. Connect to the database and get its version with pg_exec

set conn [pg_connect -conninfo "host=server.example.com dbname=template1 user=guest password=secret"]
set result [pg_exec $conn "SELECT version()"]
set value [lindex [pg_result $result -getTuple 0] 0]
pg_result $result -clear
puts "Server version is: $value"
pg_disconnect $conn