<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >Example - Bytea Escaping - Picture Viewer, Part 2 - View Pictures</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Pgtcl Reference Manual" HREF="index.html"><LINK REL="UP" TITLE="Example Programs" HREF="pgtcl-examples.html"><LINK REL="PREVIOUS" TITLE="Example - Bytea Escaping - Picture Viewer, Part 1 - Store Pictures" HREF="pgtcl-example-picstore-esc.html"><LINK REL="NEXT" TITLE="Example - Asynchronous Queries" HREF="pgtcl-example-async.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META NAME="creation" CONTENT="2004-11-09T00:53:06"></HEAD ><BODY CLASS="SECT1" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Pgtcl Reference Manual: The PostgreSQL Tcl Interface</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="pgtcl-example-picstore-esc.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 5. Example Programs</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="pgtcl-example-async.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="PGTCL-EXAMPLE-PICVIEW-ESC" >5.10. Example - Bytea Escaping - Picture Viewer, Part 2 - View Pictures</A ></H1 ><P >This example is a variation of the GIF picture viewer shown in <A HREF="pgtcl-example-picview-pq.html" >Section 5.8</A >. It also stores the images as <TT CLASS="LITERAL" >bytea</TT > fields in the database, but rather than using prepared queries in binary mode, it uses normal text mode queries. In order to get the binary data through normal SQL queries intact, the commands <A HREF="pg-escape-bytea.html" >pg_escape_bytea</A > and <A HREF="pg-unescape-bytea.html" >pg_unescape_bytea</A > are used. This version is generally less efficient that using binary prepared queries, and can be significantly slower.</P ><DIV CLASS="NOTE" ><BLOCKQUOTE CLASS="NOTE" ><P ><B >Note: </B ><CODE CLASS="FUNCTION" >pg_escape_bytea</CODE > and <CODE CLASS="FUNCTION" >pg_unescape_bytea</CODE > are extensions added to <SPAN CLASS="APPLICATION" >pgtcl-ng</SPAN > version 1.5.2 and <SPAN CLASS="APPLICATION" >pgin.tcl</SPAN > version 2.2.0. This example will not work with other versions or implementations.</P ></BLOCKQUOTE ></DIV ><P >Given an identifier on the command line, it attempts to retrieve and display the stored picture with that identifier.</P ><P >See <A HREF="pgtcl-example-picstore-esc.html" >Section 5.9</A > for the program used to insert the pictures in the database. (Since the same schema is used, the script in <A HREF="pgtcl-example-picstore-pq.html" >Section 5.7</A > can also be used.)</P ><P >This example runs under <SPAN CLASS="APPLICATION" >wish</SPAN >, not <SPAN CLASS="APPLICATION" >tclsh</SPAN >. It also assumes database connection information is provided through the environment.</P ><DIV CLASS="EXAMPLE" ><A NAME="PGTCL-EXAMPLE-PICVIEW-ESC-CODE" ></A ><P ><B >Example 5-16. Bytea Escaping - View Pictures Stored in Database</B ></P ><PRE CLASS="PROGRAMLISTING" >#!/usr/bin/wish # Example - picture storage as bytea, using escape/unescape: package require Pgtcl # Return the picture data identified by 'name'. # Throw an error if it can't be read. proc get_picture {conn name} { # Note that for a text-mode query on a BYTEA column, PostgreSQL will escape # the data for us. set result [pg_exec $conn "SELECT picture FROM pics\ WHERE pname='[pg_escape_string $name]'"] if {[pg_result $result -status] != "PGRES_TUPLES_OK"} { set message [pg_result $result -error] pg_result $result -clear error "Error: Query failed: $message" } if {[set n [pg_result $result -numTuples]] != 1} { pg_result $result -clear error "Error: Query returned $n results" } # Unescape and return the data, which is the only column in the only row: set data [pg_unescape_bytea [lindex [pg_result $result -getTuple 0] 0]] pg_result $result -clear return $data } if {$argc != 1} { puts stderr "Usage: view_picture name" exit 1 } set name [lindex $argv 0] # Connect to the database. set conn [pg_connect -conninfo ""] # Get the picture data: set failed [catch {get_picture $conn $name} data] # Done with database connection: pg_disconnect $conn # Exit if unable to retrieve the data: if {$failed} { puts "Failed to view picture '$name': $data" exit } # Make the viewer and show the picture: wm title . "Picture: $name" image create photo p -data $data -format GIF label .top -image p button .quit -text Close -command exit -default active bind . <Return> exit pack .top -side top pack .quit</PRE ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="pgtcl-example-picstore-esc.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="pgtcl-example-async.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Example - Bytea Escaping - Picture Viewer, Part 1 - Store Pictures</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="pgtcl-examples.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Example - Asynchronous Queries</TD ></TR ></TABLE ></DIV ></BODY ></HTML >