/* 

 Example Program to Select Instances from Staff table 
 This example script uses the C libraries for sql     
 The script will prompt the user to enter the sybase userid and the password
 Please beware that the password will not be conceled during entry 

*/
#include <stdio.h>
#include <stdlib.h>
#include <sybfront.h>
#include <sybdb.h>
#include <syberror.h>
#include <stdio.h>
#include <hesiod.h>


DBPROCESS * dbproc;   /* structure for the link between Sybase */
LOGINREC * login;    
char    sybase_username[20];    /* your sybase user name */
char    sybase_passwd[20];      /* your sybase password */
DBINT msgno; 

typedef struct staff_member {   /* structure to keep the results */
   char fname[50];
   char lname[50];
} staff_member;

struct staff_member staff[20];
char first_name[50];
char last_name[50];
int count=0;


/*
   MAIN
*/

main()
{
  int x=0;  /* a counter variable */


  /* Read the user name and password input from the keyboard */

  system("clear");
  printf("Example C Program with SQL \n\n");
  printf("Warning: The username and password will not be concealed at input.\n\n");

  /* Initialize user name and password entires */
  printf("Username > ");
  scanf("%s",sybase_username);
  printf("Password > ");
  scanf("%s",sybase_passwd);

  if (dbinit () == FAIL)  /* initialize the database connection */
    exit (ERREXIT);

  /* Login to the database  */
  if ((login = dblogin ()) == NULL)
    {
      printf ("can't allocate a sybase login record\n");
      exit (0);
    }	       
  
  /* set the user name */
  DBSETLUSER (login, sybase_username);
  printf ("About to DBSETLUSER for %s\n", sybase_username);

  /* set the password */
  printf ("About to DBSETLPWD for  %s\n", sybase_username);
  DBSETLPWD (login, sybase_passwd);
  if ((dbproc = dbopen (login, NULL)) == NULL)
    {
      printf ("sybase login failed\n");
      exit (0);
    }
  
  /* write out the sql command to dncmd structure */
  
  printf ("About to create SQL query\n");
  dbcmd (dbproc,"select distinct Staff.fname, Staff.lname");
  dbcmd (dbproc," from Staff");
  
  /* execute the dbcmd */
  printf ("About to execute SQL query\n");
  if (dbsqlexec (dbproc) == FAIL)
    {
      printf ("the sql command send failed\n");
      exit (0);
    }	

  printf ("About to check if the result code is SUCCEED\n");
  if (dbresults (dbproc) == SUCCEED)
    {   /* bind the results to appropriate variable names */
      dbbind (dbproc, 1, STRINGBIND, (DBINT) 0, first_name);
      dbbind (dbproc, 2, STRINGBIND, (DBINT) 0, last_name);
      /* results are returned one by one so save until end */
      while (dbnextrow (dbproc) != NO_MORE_ROWS)
	{ 
	  sprintf (staff[count].fname," %s", first_name);
	  sprintf (staff[count].lname,"%s", last_name);
	  count++;
	}
    }
  else
    {
      printf ("query failed.\n");
    }
  dbexit ();


  /* print the results */

  printf("\n\n Results : \n ---------\n");
  printf(" FName\tLName\n");
  for(x=1;x<count;x++){
    printf("%s\t%s\n",staff[x].fname,staff[x].lname);}
}



