Re: Stupid question
Posted in 1997
Richard Fremmerlid wrote:
>
> I just started using Informix about 3 months ago and have used it for
> a web application through LiveWire on HP-UX10.10. I have been using
> shell scripts with dbaccess etc to to create and load tables, but what
> I was never able to ascertain is how you would call other SQL functions
> like SELECT, DELETE, UPDATE etc from within scripts. I did all database
> processing with Server-Side Javascript through LiveWire. I have experience
> with ODBC programming in NT/95 but would like to know how to do similar c/c++
> database programming in UNIX. Could someone provide a simple example for
> me of use of SQL through scripts and c through unix. If I see a basic example
> I can probably pick up the rest from there.
> > I appreciate it.
# Create table with start date for data.
##########################################
DB="mydb"
dbaccess $DB <<EOF
set pdqpriority high;
drop table tab1;
create table tab1(
begin_date date
);
insert into tab1 values (today);
update statistics low for table tab1;
EOF
ERROR=$?
if (( ERROR != 0 )); then
printf "ERROR CREATING tab1 (${ERROR})!\\n" 2>&1
exit ${ERROR}
fi
##########################################
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
EXEC SQL INCLUDE sqltypes;
EXEC SQL BEGIN DECLARE SECTION;
char data[1024];
int value;
EXEC SQL END DECLARE SECTION;
main()
{
EXEC SQL CONNECT TO 'mydb';
if (SQLSTATE[0] != '0' ||
(SQLSTATE[1] != '0' && SQLSTATE[1] != '1'))
{
printf("connect to mydb is failed\\n");
exit(1);
}
EXEC SQL DECLARE mycursor CURSOR FOR
select * from bx;
if (strcmp(SQLSTATE, "00000"))
printf("Error In Declare Cursor\\n");
EXEC SQL OPEN mycursor;
if (strcmp(SQLSTATE, "00000"))
printf("open failed \\n");
while(1){
EXEC SQL FETCH mycursor INTO
:value;
if (strcmp(SQLSTATE,"00000")) break;
printf("%d\\n",value);
}
EXEC SQL CLOSE mycursor;
EXEC SQL FREE mycursor;
}