• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: Retrieving Images From An SQL Table

    July 10, 2017 Mike Larsen

    In the first part of this series, I showed you how to load images from the IFS into a table that has a column defined as a BLOB data type. BLOB stands for Binary Large Object and is a collection of binary data that is stored as a single entity in a database.

    Our final goal is to retrieve the images from the table we loaded in part 1 and write them back to the IFS. Once we complete this task, we should end up with the same five images we worked with in part 1. To keep everything clear, we write the images to a different folder in the IFS, one called photos_out.

    This story contains code, which you can download here.

    Table Photo_loco contains the location of the IFS folder we want to write the images to. It looks like this:

    Our program starts by executing subroutine GetOutputLocation to load the PhotoOutputLocation variable with the location where we want to write the images.

    Begsr GetOutputLocation;
    
       // get the Ifs folder we're going to write the photos to
    
       Exec sql
            Select location into :PhotoOutputLocation
               from Photo_loco;
    
    Endsr;
    

    Subroutine ProcessPhotos loops through the ‘Photos’ table and writes the images to the IFS. As a refresher, the Photos table we loaded in part 1 is shown below.

       Begsr ProcessPhotos;
    
       // read thru the table that holds the photos we want to write to the Ifs
    
        Exec sql
          Declare CsrC01 Cursor For
    
            Select NameOfPhoto
                from Photos;
    
        Exec Sql
           Close CsrC01;
    
        Exec Sql
           Open  CsrC01;
    
        DoU 1 = 0;
    
          Exec Sql
             Fetch Next From CsrC01 into :photosDs;
    
             If SqlCode < *Zeros or SqlCode = 100;
    
                If SqlCode < *Zeros;
                   //  Perform error handling
                EndIf;
    
             Exec Sql
                Close CsrC01;
    
                Leave;
    
             EndIf;
    
          // for each row we read in the photos table, write the photo out to
          // the ifs
    
          Exsr WritePhotosToIfs;
    
        Enddo;
    
       Endsr;
    

    In this subroutine, we declare and open a cursor for fetching the rows from the table. We only need the name of the photo we want to process, indicated by field NameOfPhoto. We’re fetching the name of the photo into a data structure called PhotoDs.

    // - - - -
    // photo location data structure
    
    dcl-ds photosDs; 
           photoName char(10); 
    end-ds; 
    

    To keep this example simple, I haven’t coded any error handling, but I’ve indicated where it might go. For every row we read, we’ll execute subroutine WritePhotosToIfs. There isn’t much code in this subroutine, but there is a lot happening here and it’s important to understand what’s going on.

    Begsr WritePhotosToIfs;
    
     Photo_out_FO   = SQFOVR;
     Photo_out_NAME = %trim(PhotoOutputLocation) + %trim(photoName);
     Photo_out_NL   = %Len(%TrimR(Photo_out_NAME));
    
     Exec sql
        Select Photo
          Into :Photo_Out
          From Photos
          Where NameOfPhoto = :photoName;
    
    Endsr;
    

    Before we get to the SQL select statement, it’s important to identify and understand where the fields Photo_out_FO, Photo_out_NAME, and Photo_out_NL come from. In this program, we’ve defined a variable called Photo_out that is defined with a SQL_type(BLOB_file) data type.

    dcl-s Photo_out           sqltype(Blob_file);

    When the program is compiled, the SQL precompiler converts Photo_out to a data structure in the following format:

    // The SQLTYPE(BLOB_FILE) definition will be converted by the compiler
    // into the following data structure:                                 
                                                                          
    //   D PHOTO_OUT        DS                                            
    //   D PHOTO_OUT_NL                   10U 0                           
    //   D PHOTO_OUT_DL                   10U 0                           
    //   D PHOTO_OUT_FO                   10U 0                           
    //   D PHOTO_OUT_NAME                255A   CCSID(*JOBRUNMIX)

    Let’s break down the meaning of each of these.

    • PHOTO_OUT_NAME is the name of the IFS file we’re creating. In this example, I made this the IFS output location plus the photo name.
    • PHOTO_OUT_NL is the length of the value from PHOTO_OUT_NAME.
    • PHOTO_OUT_DL is an output field and stands for data length. We’re not doing anything with this field in our program.
    • PHOTO_OUT_FO is the type of the file open. We set this field to SQFOVR, which means create a new file or replace an existing one.

    Once we populate the data structure with the proper values, we issue the SQL select statement that creates the image in the IFS.

    After running this program, we can see our images loaded in the IFS folder ‘/mikel/photos’.

    So, we’re done, right? Not exactly. Let’s download and open one of the photos to make sure we can still view the actual image. I’m going to use IBM i Access Client Solutions to download the file, then open it up to view it with Windows Photos. A look at the ‘lexie1.png’ image shows we have the same image that we loaded into the SQL table in part 1 of this series.

    Working with images and other media types is pretty easy once you understand how to do it. I’m sure you can come up with other uses of this code. This concludes the series on storing and retrieving images on the IBM i. I hope you found this series both informative and helpful.

    RELATED STORY

    Storing Images In An SQL Table

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: Binary Large Object, BLOB, IBM i, SQL

    Sponsored by
    WorksRight Software

    Do you need area code information?
    Do you need ZIP Code information?
    Do you need ZIP+4 information?
    Do you need city name information?
    Do you need county information?
    Do you need a nearest dealer locator system?

    We can HELP! We have affordable AS/400 software and data to do all of the above. Whether you need a simple city name retrieval system or a sophisticated CASS postal coding system, we have it for you!

    The ZIP/CITY system is based on 5-digit ZIP Codes. You can retrieve city names, state names, county names, area codes, time zones, latitude, longitude, and more just by knowing the ZIP Code. We supply information on all the latest area code changes. A nearest dealer locator function is also included. ZIP/CITY includes software, data, monthly updates, and unlimited support. The cost is $495 per year.

    PER/ZIP4 is a sophisticated CASS certified postal coding system for assigning ZIP Codes, ZIP+4, carrier route, and delivery point codes. PER/ZIP4 also provides county names and FIPS codes. PER/ZIP4 can be used interactively, in batch, and with callable programs. PER/ZIP4 includes software, data, monthly updates, and unlimited support. The cost is $3,900 for the first year, and $1,950 for renewal.

    Just call us and we’ll arrange for 30 days FREE use of either ZIP/CITY or PER/ZIP4.

    WorksRight Software, Inc.
    Phone: 601-856-8337
    Fax: 601-856-9432
    Email: software@worksright.com
    Website: www.worksright.com

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Counting Companies With An IBM i In The Mix IBM Commits To Power9 Upgrades For Big Power Systems Shops

    Leave a Reply Cancel reply

TFH Volume: 27 Issue: 45

This Issue Sponsored By

  • Profound Logic Software
  • Linoma Software
  • Fresche
  • COMMON
  • WorksRight Software

Table of Contents

  • Vision Buys Enforcive, Then Gets Sold And Merged With Syncsort
  • IBM Commits To Power9 Upgrades For Big Power Systems Shops
  • Guru: Retrieving Images From An SQL Table
  • Counting Companies With An IBM i In The Mix
  • Pride In Advancing The IBM i Community

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24
  • Big Blue Raises IBM i License Transfer Fees, Other Prices
  • Keep The IBM i Youth Movement Going With More Training, Better Tools
  • Remain Begins Migrating DevOps Tools To VS Code
  • IBM Readies LTO-10 Tape Drives And Libraries
  • IBM i PTF Guide, Volume 27, Number 23

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle