database_doc: Large objects

Next: Copy in, Up: Examples [Index]

#### 6.4.1 Large objects

    # connect
    conn = pq_connect (setdbopts ("dbname", "test"));

    # create table to store large object oids
    pq_exec_params (conn, "create table programs (name text, image oid);")

    # start transaction, so we don't miss storing the oid
    pq_exec_params (conn, "begin;")

    # download image, pipe it to a large objects and note its oid
    # (you need `wget' to be installed)
    oid = pq_lo_import (conn,
    "wget -q -O - https://www.gnu.org/software/octave/images/screenshot.png |")

    # write oid and program name into the table
    pq_exec_params (conn,
                    "insert into programs values ($1, $2);", {"Octave", oid})

    # finish transaction
    pq_exec_params (conn, "commit;")

    # find and retrieve the oid searching for the program name
    foid = pq_exec_params (conn,
                           "select image from programs where name = $1;",
                           {"Octave"}).data{1}

    # view large object corresponding to found foid
    # (you need `eog' to be installed, or change to another program able to
    # display `.png')
    pq_lo_view (conn, foid, "eog")

    # cleanup (we don't use `begin;' and `rollback;' since the
    # example should show an inner transaction instead)

    pq_lo_unlink (conn, oid);

    pq_exec_params (conn, "drop table programs;");

    pq_close (conn);