#include <stdio.h>
#include "clib.h"
#include "cns_data_structs.h"
#include "acnet_errors.h"

int main(void)

{

char my_name[30];
int acnet_status;
unsigned int rows;
unsigned int bytes_returned;
db_handle_t *handle;
DATA_STRUCT *raw_data;

  acnet_status = db_open(&handle,"ADBS","appdb",300);
  if (acnet_status != OK)
      {
      (void)printf("%s",db_error(handle));
      return (acnet_status);
      }

  rows = 1;

  acnet_status = db_select(handle,"select name from users where id=1",
			   &rows,sizeof(my_name),my_name);
  if ((acnet_status != SQL_MOREROWS) && (acnet_status != OK))
      {
      (void)printf("%s",db_error(handle));
      db_close(handle);
      return (acnet_status);
      }

  if (acnet_status == SQL_MOREROWS)
  printf("more than one name exists for id=1\n");

  if (rows == 1)
      printf("one name for id=1 is %30.30s\n",my_name);
  else
      printf("no name exists for id=1\n");

  acnet_status = db_modify(handle,"update users set name='new' where id=1",
			   &rows);
  if (acnet_status != OK)
      {
      (void)printf("%s",db_error(handle));
      db_close(handle);
      return (acnet_status);
      }

  printf("%d name(s) for id=1 were set to 'new'\n",rows);

  rows = 0;

  acnet_status = db_select(handle,"select col1, col2, col4 from table",
			   &rows,(unsigned int) 0,(void *) 0);
  raw_data = (DATA_STRUCT *) malloc(sizeof(DATA_STRUCT));
  while (!(acnet_status = db_nextrow(handle,sizeof(DATA_STRUCT),raw_data,
				     &bytes_returned)))
      {
      if (bytes_returned == 0)
      break;
    /* process raw_data here */
      }

  if (acnet_status != OK)
      {
      (void)printf("%s",db_error(handle));
      db_close(handle);
      return (acnet_status);
      }

  db_close(handle);

}