Re: VARCHAR strings in Oracle 7?

From: Tim House <thouse_at_chiba.demon.co.uk>
Date: 1995/09/30
Message-ID: <812499756.26136_at_chiba.demon.co.uk>#1/1


patrick_at_casbs.Stanford.EDU (Patrick Goebel) wrote:
>
>I am trying to write a very simple Pro*C procedure under 7.1.3 on a
>Sun SPARC 5, SunOS 4.1.3. All I want to do is retrieve a person's
>first name, given the last name. Under SQL*Plus, the following select
>statement works fine:
>
>select name_first
>from PROFILE
>where name_last = 'GOEBEL'
>;
>
>NAME_FIRST
>-------------------------
>PATRICK
>
>However, the following Pro*C code (which compiles without error) fails
>to retrieve the data -- I get a "Not Found" condition -- when given
>the string GOEBEL as input.
>
>Can anyone see what I am doing wrong. It seems so simple!
>
> -------- Begin Pro*C code ---------
>
>#include <stdio.h>
>#include <ctype.h>
>
>varchar oracle_user[20];
>varchar oracle_password[20];
>varchar name_last[25];
>varchar name_first[25];
>
>EXEC SQL INCLUDE sqlca.h;
>
>void sql_error();
>
>main(argc, argv)
> int argc;
> char *argv[];
>
>{
> strcpy((char *) oracle_user.arr, "oracle", 20);
> oracle_user.len = strlen((char *) oracle_user.arr);
> strcpy((char *) oracle_password.arr, "*****", 20);
> oracle_password.len = strlen((char *) oracle_password.arr);
>
> strcpy((char *) name_last.arr, argv[1], 25);
> name_last.len = strlen((char *) name_last.arr);
> name_last.arr[name_last.len] = '\0';
>
> EXEC SQL WHENEVER SQLERROR DO sql_error("ORACLE error--\n");
>
> EXEC SQL CONNECT :oracle_user IDENTIFIED BY :oracle_password;
>
> EXEC SQL WHENEVER NOT FOUND GOTO notfound;
>
> EXEC SQL SELECT name_first
> INTO :name_first
> FROM PROFILE
> WHERE name_last = :name_last;
>
> printf("%s",name_first.arr);
> exit(0);
>
>notfound:
> printf("-1");
> exit(1);
>}
>
>void
>sql_error(msg)
>char *msg;
>{
> char err_msg[128];
> int buf_len, msg_len;
>
> EXEC SQL WHENEVER SQLERROR CONTINUE;
>
> printf("\n%s\n", msg);
> buf_len = sizeof (err_msg);
> sqlglm(err_msg, &buf_len, &msg_len);
> printf("%.*s\n", msg_len, err_msg);
>
> EXEC SQL ROLLBACK RELEASE;
> exit(1);
>}

What we actually have here is a very common C problem - failure to null terminate

Inserting the line ...

name_first.arr[name_first.len]=0 ;

prior to the line ...

printf("%s",name_first.arr);

should ensure that the string to be printed ends with the null character that printf expects - otherwise it will charge on looking fo one.

Good luck

Tim House
DBA Warner Music UK Ltd Received on Sat Sep 30 1995 - 00:00:00 CET

Original text of this message