Please Help with small program

This is a discussion on Please Help with small program within the C Programming forums, part of the General Programming Boards category; I am having alot of trouble getting my code to read the first 4 lines of this database table and ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Please Help with small program

    I am having alot of trouble getting my code to read the first 4 lines of this database table and print the results to the screen. Can someone please help me make proper corrections to my code. My sql statement is failing and I am very confused. There is some error handling functions in here which I can get thru but my main problen is passing the records thru an array of structures and printing those results to the screen. I would like to read the entire table instead of just 4 rows but I am thinking that will take longer. Thanks.

    Code:
    #include <stdio.h>
    #include<stdlib.h>
    #include<strings.h>
    #include<string.h>
    #include"imcs_params.h"
    #include"jnl_struct.h"
    #include"misc.h"
    #include"err_supv.h"
    
    
     /*Define structure to hold database table record */
     struct cc_lkup_ {
        short   cc;
        char    cc_suf[2];
        char    cc_sn[11];
        char    cc_desc[41];
        short   prd_rpt_id;
        short   rpt_cc;
        char    sfdc_grp[41];
        char    cc_grp[41];
        char    proc_grp[3];
        char    proc_grp_desc[16];
        short   cc_typ;
        char    resp_company[4];
        char    rpt_tbl[33];
      } cc_lkup_;
    
    
    main ()
    {
     int reccnt = 0;
     JNL_STRUCT   jnl;
    
    
    /*Zero out structure - Initialize*/
     memset (&cc_lkup_, 0, sizeof(cc_lkup_));
    
    
    /* Get records from database table psr_skid_mkup
    EXEC SQL SELECT cc, cc_sn, cc_desc, cc_grp
             INTO :cc, :cc_sn, :cc_desc, :cc_grp
             FROM cc_lkup */
    
    /* Verify no errors were encountered during SELECT statement */
     status = err_supv(apl_nm, prog_nm, jnl.blk, stmt_purpose, &retry_cnt,
              &err_num, err_txt, &row_count, jnl.val1, jnl.val2, popup_flag);
    
     if (status != IMCS_DB_SUCCESS) return status;
    
    /* PRINT TO SCREEN */
     int i;
    
    
     for( i = 0; i < 4; i++ )
      {
      printf("COST CENTER NUMBER:%d  LOCATION:%s  DESCRIPTION:%s  GROUP NUMBER:%s\n", cc_lkup_[i].cc, cc_lkup_[i].cc_sn, cc_lkup_[i].cc_desc, cc_lkup_[i].cc_grp);
      }
    
    }

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Please ignore psr_skid_mkup

    That should say cc_lkup_

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    I would have trouble too with such cryptic variable names and bad formatting. There are also some header files which I don't think are standard, do you reckon we need to know them?

    Also where is this defined:

    JNL_STRUCT
    =========================================
    Everytime you segfault, you murder some part of the world

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    cc_lkup_[i].cc
    Perhaps I've gone blind, but I don't see where you have an array of these structs?

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Its included in makefile

    JNL STRUCT is below:
    It is just used to write events to the log file

    Code:
    #define _JNL_STRUCT_
    
    typedef struct {
        int     error;
        char    grp[3];
        int     class;
        char    mod[13];
        int     blk;
        char    mesg[4096];
        int     val1;
        int     val2;
    } JNL_STRUCT;
    I have to include these things and more for proper error handling once I figure out how to use a array of structures (cc_lkup_) in my code to read database table. Thanks for loking at this for me and any questions I will answer right away.

    -Carl

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Updated Code

    Code:
    #include <stdio.h>
    #include<stdlib.h>
    #include<strings.h>
    #include<string.h>
    #include"imcs_params.h"
    #include"jnl_struct.h"
    #include"misc.h"
    #include"err_supv.h"
    
    /*Define structure to hold database table record */
     struct cc_lkup_ {
        short   cc;
        char    cc_suf[2];
        char    cc_sn[11];
        char    cc_desc[41];
        short   prd_rpt_id;
        short   rpt_cc;
        char    sfdc_grp[41];
        char    cc_grp[41];
        char    proc_grp[3];
        char    proc_grp_desc[16];
        short   cc_typ;
        char    resp_company[4];
        char    rpt_tbl[33];
      } cc_lkup_;
    
    
    int main ()
    {
     int reccnt = 0;
     JNL_STRUCT   jnl;
    
    
    /*Zero out structure - Initialize*/
     memset (&cc_lkup_, 0, sizeof(cc_lkup_));
    
    /*Determine how many records are in the table
    EXEC SQL SELECT COUNT(*) INTO :count:nind
                FROM cc_lkup*/
    
    /* Get records from database table psr_skid_mkup
    EXEC SQL SELECT cc, cc_sn, cc_desc, cc_grp
             INTO :cc, :cc_sn, :cc_desc, :cc_grp
             FROM cc_lkup */
    
    /* Verify no errors were encountered during SELECT statement */
     status = err_supv(apl_nm, prog_nm, jnl.blk, stmt_purpose, &retry_cnt,
              &err_num, err_txt, &row_count, jnl.val1, jnl.val2, popup_flag);
    
    /* EXIT PROGRAM IF ERROR SELECTING RECORDS*/
     if (status != IMCS_DB_SUCCESS)
         exit(1);
    
    /*WRITE PURPOSE OF CODE TO THE LOG FILE*/
    jnl.blk = 1999;
    sprintf(stmt_purpose,"testing array of structures by Carl J");
    log_it(log_file, jnl);
    
    /* PRINT TO SCREEN */
     int i;
    
    /*READ ALL RECORDS FOUND IN TABLE - TOTAL IS STORED IN variable COUNT*/
     for( i = 0; i < count; i++ )
      {
      printf("COST CENTER NUMBER:%d  LOCATION:%s  DESCRIPTION:%s  GROUP NUMBER:%s\n", cc_lkup_[i].cc, cc_lkup_[i].cc_sn, cc_lkup_[i].cc_desc, cc_lkup_[i].cc_grp);
      }
    
    return (0);
    }

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
     struct cc_lkup_ {
        short   cc;
        char    cc_suf[2];
        char    cc_sn[11];
        char    cc_desc[41];
        short   prd_rpt_id;
        short   rpt_cc;
        char    sfdc_grp[41];
        char    cc_grp[41];
        char    proc_grp[3];
        char    proc_grp_desc[16];
        short   cc_typ;
        char    resp_company[4];
        char    rpt_tbl[33];
      } cc_lkup_[4];
    You still only have one struct, not an array of them.

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You are doing a SELECT INTO, which returns 1 row, and the data is being placed into your structure that is defined as a global variable. Therefore, there is no reason to loop 4 times and index through an array of structures (since you don't have an array - you just have one instance).

    It might be helpful if you told us if you are having compile issues or runtime issues, and what those particular errors are. By the looks of it, you are still having compile errors.
    Last edited by Dino; 04-14-2008 at 09:38 AM.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by tabstop View Post
    You still only have one struct, not an array of them.
    And he doesn't need an array, since with that SELECT INTO statement he will only ever get one row returned.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Yes - You are right as usual - Sorry about that

    I removed the INTO and made a few other changes. The code is below. I inserted SQL BEGIN and SQL END. I believe that and removing INTO will allow me to read all the records instead of just one. I then need to send all the records to my structure (cc_lkup) and print them to the screen. Thanks in advance for any help. I know my problem is with the aray and selecting all the records but I am trying very hard to understand this.

    Code:
     /*Define structure to hold database table record */
     struct cc_lkup_ {
        short   cc;
        char    cc_suf[2];
        char    cc_sn[11];
        char    cc_desc[41];
        short   prd_rpt_id;
        short   rpt_cc;
        char    sfdc_grp[41];
        char    cc_grp[41];
        char    proc_grp[3];
        char    proc_grp_desc[16];
        short   cc_typ;
        char    resp_company[4];
        char    rpt_tbl[33];
      } cc_lkup_;
    
    
    int main ()
    {
     int reccnt = 0;
     int totalcount = 0;
     JNL_STRUCT   jnl;
    
    
    /*Zero out structure - Initialize*/
     memset (&cc_lkup_, 0, sizeof(cc_lkup_));
    
    /*Determine how many records are in the table
    EXEC SQL SELECT COUNT(*) INTO :count:nind
                FROM cc_lkup*/
    
    /* Verify no errors were encountered during SELECT statement */
     status = err_supv(apl_nm, prog_nm, jnl.blk, stmt_purpose, &retry_cnt,
              &err_num, err_txt, &row_count, jnl.val1, jnl.val2, popup_flag);
    
    /* EXIT PROGRAM IF ERROR COUNTING RECORDS*/
     if (status != IMCS_DB_SUCCESS)
         exit(1);
    
    /*PLACE TOTAL NUMBER OF RECORDS FOUND IN TOTALCOUNT VARIABLE*/
    totalcount = count;
    
    /*WRITE PURPOSE OF CODE TO THE LOG FILE*/
    jnl.blk = 1899;
    sprintf(stmt_purpose,"counting all records in cc_lkup by Carl J");
    log_it(log_file, jnl);
    
    /* Get records from database table psr_skid_mkup
    EXEC SQL SELECT cc, cc_sn, cc_desc, cc_grp
             INTO :cc, :cc_sn, :cc_desc, :cc_grp
             FROM cc_lkup */
    
    /* Verify no errors were encountered during SELECT statement */
     status = err_supv(apl_nm, prog_nm, jnl.blk, stmt_purpose, &retry_cnt,
              &err_num, err_txt, &row_count, jnl.val1, jnl.val2, popup_flag);
    
    /* EXIT PROGRAM IF ERROR SELECTING RECORDS*/
     if (status != IMCS_DB_SUCCESS)
         exit(1);
    
    /*WRITE PURPOSE OF CODE TO THE LOG FILE*/
    jnl.blk = 1999;
    sprintf(stmt_purpose,"selecting all records in the table cc_lkup by Carl J");
    log_it(log_file, jnl);
    
    /* PRINT TO SCREEN */
     int i;
    
    /*READ ALL RECORDS FOUND IN TABLE - TOTAL IS STORED IN variable COUNT*/
     for( i = 0; i < totalcount; i++ )
      {
      printf("COST CENTER NUMBER:%d  LOCATION:%s  DESCRIPTION:%s  GROUP NUMBER:%s\n", cc_lkup_[i].cc, cc_lkup_[i].cc_sn, cc_lkup_[i].cc_desc, cc_lkup_[i].cc_grp);
      }
    
    return (0);
    }

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    Updated code below - Need to slow down

    Code:
    /*Define structure to hold database table record */
     struct cc_lkup_ {
        short   cc;
        char    cc_suf[2];
        char    cc_sn[11];
        char    cc_desc[41];
        short   prd_rpt_id;
        short   rpt_cc;
        char    sfdc_grp[41];
        char    cc_grp[41];
        char    proc_grp[3];
        char    proc_grp_desc[16];
        short   cc_typ;
        char    resp_company[4];
        char    rpt_tbl[33];
      } cc_lkup_;
    
    
    int main ()
    {
     int reccnt = 0;
     int totalcount = 0;
     JNL_STRUCT   jnl;
    
    
    /*Zero out structure - Initialize*/
     memset (&cc_lkup_, 0, sizeof(cc_lkup_));
    
    /*Determine how many records are in the table
    EXEC SQL SELECT COUNT(*) INTO :count:nind
                FROM cc_lkup*/
    
    /* Verify no errors were encountered during SELECT statement */
     status = err_supv(apl_nm, prog_nm, jnl.blk, stmt_purpose, &retry_cnt,
              &err_num, err_txt, &row_count, jnl.val1, jnl.val2, popup_flag);
    
    /* EXIT PROGRAM IF ERROR COUNTING RECORDS*/
     if (status != IMCS_DB_SUCCESS)
         exit(1);
    
    /*PLACE TOTAL NUMBER OF RECORDS FOUND IN TOTALCOUNT VARIABLE*/
    totalcount = count;
    
    /*WRITE PURPOSE OF CODE TO THE LOG FILE*/
    jnl.blk = 1899;
    sprintf(stmt_purpose,"counting all records in cc_lkup by Carl J");
    log_it(log_file, jnl);
    
    /* Get records from database table psr_skid_mkup
    EXEC SQL SELECT cc, cc_sn, cc_desc, cc_grp
                                ????? :cc, :cc_sn, :cc_desc, :cc_grp
                                FROM cc_lkup 
                                SQL BEGIN;
                                SQL END;*/
    
    /* Verify no errors were encountered during SELECT statement */
     status = err_supv(apl_nm, prog_nm, jnl.blk, stmt_purpose, &retry_cnt,
              &err_num, err_txt, &row_count, jnl.val1, jnl.val2, popup_flag);
    
    /* EXIT PROGRAM IF ERROR SELECTING RECORDS*/
     if (status != IMCS_DB_SUCCESS)
         exit(1);
    
    /*WRITE PURPOSE OF CODE TO THE LOG FILE*/
    jnl.blk = 1999;
    sprintf(stmt_purpose,"selecting all records in the table cc_lkup by Carl J");
    log_it(log_file, jnl);
    
    /* PRINT TO SCREEN */
     int i;
    
    /*READ ALL RECORDS FOUND IN TABLE - TOTAL IS STORED IN variable COUNT*/
     for( i = 0; i < totalcount; i++ )
      {
      printf("COST CENTER NUMBER:%d  LOCATION:%s  DESCRIPTION:%s  GROUP NUMBER:%s\n", cc_lkup_[i].cc, cc_lkup_[i].cc_sn, cc_lkup_[i].cc_desc, cc_lkup_[i].cc_grp);
      }
    
    return (0);
    }

  12. #12
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Updated code below - Need to slow down
    Need to read a book!

    Still several problems - you are going from bad to worse.

    When you had a SELECT INTO, things were simpler. Now that you have removed the INTO clause, your programming model must change to a different paradigm.

    First, you have to do a DECLARE CURSOR.

    Then you OPEN the CURSOR.

    Then, you FETCH the cursor for however many rows you want or until a SQLCODE=100 (no more rows)

    Then, you close the cursor.

    Something like this:
    Code:
    EXEC SQL DECLARE CURSOR CURSOR1 FOR SELECT cc, cc_sn, cc_desc, cc_grp
             FROM cc_lkup ;     
                            
    EXEC SQL OPEN CURSOR1 ; 
    check for success 
    
    Loop: 
    EXEC SQL FETCH CURSOR1 INTO  :cc, :cc_sn, :cc_desc, :cc_grp ; 
    check for success 
    if still more rows, goto loop ; 
    
    EXEC SQL CLOSE CURSOR1 ; 
    check for success
    Last edited by Dino; 04-14-2008 at 10:00 AM. Reason: P.S. check your SQL Reference and Application Programming Guide for proper syntax.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Program to reverse a number. small error with it..
    By comproghelp in forum C Programming
    Replies: 8
    Last Post: 11-22-2004, 09:52 AM
  3. Help writing small program
    By guitarhead2000 in forum C++ Programming
    Replies: 2
    Last Post: 10-13-2004, 12:42 PM
  4. A little Help with a small program
    By whtpirate in forum C Programming
    Replies: 7
    Last Post: 06-05-2003, 06:15 PM
  5. Very small program...Whats wrong???
    By SmokingMonkey in forum C++ Programming
    Replies: 4
    Last Post: 05-30-2003, 09:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21