C语言嵌入informix基础入门示例讲解

  #include

  #include

  #include

  #include

  $struct _db_person

  {

  char name[30+1];

  char card[12+1];

  int age;

  };

  char *trim(char *str)

  {

  char *p, *buffer;

  int len;

  if( NULL!=str )

  {

  len = strlen(str);

  if( len > 0 )

  {

  buffer=(char *)malloc(sizeof(char)*(len+1));

  if( NULL != buffer )

  {

  memmove(buffer, str, len);

  buffer[len]=0;

  p = buffer + len - 1;

  while( (p != buffer) && ((*p > 0x00) && (*p <= ' ')) )

  *(p--) = 0;

  p = buffer;

  while( (*p > 0x00) && (*p <= ' ') )

  p++;

  strcpy(str, p);

  free(buffer);

  }

  }

  }

  return str;

  }

  int GetData(struct _db_person *pps)

  {

  char strage[20];

  memset(pps, 0, sizeof(struct _db_person));

  printf("enter name: ");

  fgets(pps->name,sizeof(pps->name),stdin);

  trim(pps->name);

  if( strlen(pps->name) == 0 )

  return -1;

  printf("enter card no: ");

  fgets(pps->card,sizeof(pps->card),stdin);

  trim(pps->card);

  if( strlen(pps->card) == 0 )

  return -2;

  printf("enter age: ");

  fgets(strage,sizeof(strage),stdin);

  trim(strage);

  if( strlen(strage) == 0 )

  return -3;

  pps->age = atoi(strage);

  return 0;

  }

  int main(void)

  {

  $struct _db_person dbps;

  $database exec01;

  if( SQLCODE != 0 )

  {

  printf("open demo1 failure,SQLCODE=%d

  ",SQLCODE);

  return -1;

  }

  while( 1 )

  {

  if( GetData(&dbps)<0 )

  break;

  $insert into person(name, card, age) values($dbps.name, $dbps.card, $dbps.age);

  printf("insert data result: SQLCODE=%d

  ",SQLCODE);

  $declare vcursor cursor for select name, card, age into $dbps.name, $dbps.card, $dbps.age from person;

  printf("declare vcursor result: SQLCODE=%d

  ",SQLCODE);

  $open vcursor;

  printf("open vcursor result: SQLCODE=%d

  ",SQLCODE);

  if( 0==SQLCODE )

  {

  while( 1 )

  {

  $fetch vcursor;

  if( 0==SQLCODE )

  {

  printf("name=[%s],card=[%s],age=[%d]

  ",dbps.name,dbps.card,dbps.age);

  }

  else

  {

  if( SQLCODE==100 )

  printf("fetch end!

  ");

  else

  printf("fetch failure!SQLCODE=%d

  ",SQLCODE);

  break;

  }

  }

  }

  $close vcursor;

  $free vcursor;

  }

  $disconnect current;

  return 0;

  }