Sophie

Sophie

distrib > CentOS > 5 > x86_64 > by-pkgid > dc5bd15dd837bfdf58139cb74856b967 > files > 45

postgresql-tcl-8.1.23-10.el5_10.x86_64.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Example - Large Objects - Picture Viewer, Part 1 - Store 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 - The Different Ways to Get Query Results"
HREF="pgtcl-example-results.html"><LINK
REL="NEXT"
TITLE="Example - Large Objects - Picture Viewer, Part 2 - View Pictures"
HREF="pgtcl-example-picview-lo.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-results.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-picview-lo.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="PGTCL-EXAMPLE-PICSTORE-LO"
>5.5. Example - Large Objects - Picture Viewer, Part 1 - Store Pictures</A
></H1
><P
>This example is the first part of a GIF picture viewer which stores
the images as large objects in the database. Given pairs of
identifiers (arbitrary names used as keys) and filenames on the command line,
it inserts the contents of each file into the database as a large object,
and inserts a record in a table to allow retrieving the large object
using the provided identifier as the key.</P
><P
>See <A
HREF="pgtcl-example-picview-lo.html"
>Section 5.6</A
> for the program used to view
the pictures.  A different implementation using <TT
CLASS="LITERAL"
>bytea</TT
>
fields instead of large objects can be found in
<A
HREF="pgtcl-example-picstore-pq.html"
>Section 5.7</A
>.
and <A
HREF="pgtcl-example-picstore-esc.html"
>Section 5.9</A
>.</P
><P
>The schema is created automatically if the table is not found. This method
wouldn't normally be used in production, though. The example assumes
database connection information is provided through the environment.</P
><DIV
CLASS="EXAMPLE"
><A
NAME="PGTCL-EXAMPLE-PICSTORE-LO-CODE"
></A
><P
><B
>Example 5-11. Large Objects - Store Pictures in Database</B
></P
><PRE
CLASS="PROGRAMLISTING"
>#!/usr/bin/tclsh
# Example - picture storage as Large Object - importer

package require Pgtcl

# Build the table schema:
proc build {conn} {
    pg_execute $conn "CREATE TABLE pics (pname TEXT PRIMARY KEY, poid OID)"
}

# Insert file 'file' into the database with key 'name':
# If an error occurs, throws a Tcl error.
proc insert_file {conn name file } {
    set qname [pg_escape_string $name]
    pg_execute $conn BEGIN
    if {[catch {
        set oid [pg_lo_import $conn $file]
        pg_execute $conn "INSERT INTO pics (poid, pname) VALUES ($oid, '$qname')"
    } msg]} {
        pg_execute $conn ROLLBACK
        error "Error importing large object: $msg"
    }
    pg_execute $conn COMMIT
}

if {$argc &lt; 2 || $argc % 2 != 0} {
    puts stderr "Usage: insert_picture name filename \[name filename\]..."
    exit 1
}

# Connect to the database.
set conn [pg_connect -conninfo ""]

# Check for table:
if {[catch {pg_execute $conn "SELECT COUNT(*) AS n FROM pics"} msg]} {
     puts "Note: unable to select from table. Let's try creating it."
     build $conn
}

# Insert all the pictures named on the command line:
foreach {name filename} $argv {
    if {[catch {insert_file $conn $name $filename} message]} {
        puts "$filename NOT inserted: $message"
    } else {
        puts "$filename inserted OK as '$name'"
    }
}

pg_disconnect $conn</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-results.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-picview-lo.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Example - The Different Ways to Get Query Results</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 - Large Objects - Picture Viewer, Part 2 - View Pictures</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>