| 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" |
| 2 | "http://www.w3.org/TR/html4/loose.dtd"> |
| 3 | <html> |
| 4 | <head> |
| 5 | <meta name="generator" content="HTML Tidy for HTML5 for Linux version 5.6.0"> |
| 6 | <title>Example Program</title> |
| 7 | <meta name="GENERATOR" content="Modular DocBook HTML Stylesheet Version 1.79"> |
| 8 | <link rev="MADE" href="mailto:[email protected]"> |
| 9 | <link rel="HOME" title="PostgreSQL Tcl Interface Documentation" href="index.html"> |
| 10 | <link rel="UP" title="pgtcl - Tcl Binding Library" href="libpgtcl.html"> |
| 11 | <link rel="PREVIOUS" title="Connection/result handles as commands" href="pgtcl-commandhandles.html"> |
| 12 | <link rel="STYLESHEET" type="text/css" href="stylesheet.css"> |
| 13 | </head> |
| 14 | <body class="SECT1"> |
| 15 | <div class="NAVHEADER"> |
| 16 | <table summary="Header navigation table" width="100%" border="0" cellpadding="0" cellspacing="0"> |
| 17 | <tr> |
| 18 | <th colspan="5" align="center" valign="bottom">PostgreSQL Tcl Interface Documentation</th> |
| 19 | </tr> |
| 20 | <tr> |
| 21 | <td width="10%" align="left" valign="top"><a href="pgtcl-commandhandles.html" accesskey="P">Prev</a></td> |
| 22 | <td width="10%" align="left" valign="top"><a href="libpgtcl.html">Fast Backward</a></td> |
| 23 | <td width="60%" align="center" valign="bottom">Chapter 1. <span class="APPLICATION">pgtcl</span> - Tcl Binding Library</td> |
| 24 | <td width="10%" align="right" valign="top"><a href="libpgtcl.html">Fast Forward</a></td> |
| 25 | <td width="10%" align="right" valign="top"> </td> |
| 26 | </tr> |
| 27 | </table> |
| 28 | <hr align="left" width="100%"></div> |
| 29 | <div class="SECT1"> |
| 30 | <h1 class="SECT1"><a name="PGTCL-EXAMPLESECT" id="PGTCL-EXAMPLESECT">1.6. Example Program</a></h1> |
| 31 | <p><a href="pgtcl-examplesect.html#PGTCL-EXAMPLE">Example 1-1</a>> shows a small example of how to use the <span class="APPLICATION">pgtcl</span> commands.</p> |
| 32 | <div class="EXAMPLE"><a name="PGTCL-EXAMPLE" id="PGTCL-EXAMPLE"></a> |
| 33 | <p><b>Example 1-1. <span class="APPLICATION">pgtcl</span> Example Program</b></p> |
| 34 | <pre class="PROGRAMLISTING"># getDBs : |
| 35 | # get the names of all the databases at a given host and port number |
| 36 | # with the defaults being the localhost and port 5432 |
| 37 | # return them in alphabetical order |
| 38 | proc getDBs { {host "localhost"} {port "5432"} } { |
| 39 | # datnames is the list to be result |
| 40 | set conn [pg_connect template1 -host $host -port $port] |
| 41 | set res [pg_exec $conn "SELECT datname FROM pg_database ORDER BY datname;"] |
| 42 | set ntups [pg_result $res -numTuples] |
| 43 | for {set i 0} {$i < $ntups} {incr i} { |
| 44 | lappend datnames [pg_result $res -getTuple $i] |
| 45 | } |
| 46 | pg_result $res -clear |
| 47 | pg_disconnect $conn |
| 48 | return $datnames |
| 49 | } |
| 50 | |
| 51 | ## OR an alternative |
| 52 | |
| 53 | proc getDBs { {host "localhost"} {port "5432"} } { |
| 54 | # datnames is the list to be result |
| 55 | set conn [pg_connect template1 -host $host -port $port] |
| 56 | set res [pg_exec $conn "SELECT datname FROM pg_database ORDER BY datname;"] |
| 57 | |
| 58 | set datnames [pg_result $res -list] |
| 59 | pg_result $res -clear |
| 60 | pg_disconnect $conn |
| 61 | return $datnames |
| 62 | } |
| 63 | |
| 64 | ## OR an alternative |
| 65 | |
| 66 | proc getDBs { {host "localhost"} {port "5432"} } { |
| 67 | # datnames is the list to be result |
| 68 | set conn [pg_connect template1 -host $host -port $port] |
| 69 | set res [$conn exec "SELECT datname FROM pg_database ORDER BY datname;"] |
| 70 | |
| 71 | set datnames [$res -dict] |
| 72 | $res -clear |
| 73 | rename $conn {} |
| 74 | return [dict get $datnames] |
| 75 | } </pre></div> |
| 76 | </div> |
| 77 | <div class="NAVFOOTER"> |
| 78 | <hr align="left" width="100%"> |
| 79 | <table summary="Footer navigation table" width="100%" border="0" cellpadding="0" cellspacing="0"> |
| 80 | <tr> |
| 81 | <td width="33%" align="left" valign="top"><a href="pgtcl-commandhandles.html" accesskey="P">Prev</a></td> |
| 82 | <td width="34%" align="center" valign="top"><a href="index.html" accesskey="H">Home</a></td> |
| 83 | <td width="33%" align="right" valign="top"> </td> |
| 84 | </tr> |
| 85 | <tr> |
| 86 | <td width="33%" align="left" valign="top">Connection/result handles as commands</td> |
| 87 | <td width="34%" align="center" valign="top"><a href="libpgtcl.html" accesskey="U">Up</a></td> |
| 88 | <td width="33%" align="right" valign="top"> </td> |
| 89 | </tr> |
| 90 | </table> |
| 91 | </div> |
| 92 | </body> |
| 93 | </html> |