Advertisement
Guest User

Untitled

a guest
Oct 20th, 2013
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.49 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <sys/param.h>
  3. #include <sybfront.h>
  4. #include <sybdb.h>
  5. #include <syberror.h>
  6. #include <stdio.h>
  7.  
  8. #define  UID       ""
  9. #define  PWD       ""
  10. #define  PROGNAME  ""
  11. #define  DBSERVER  ""
  12. #define  DBNAME    ""
  13.  
  14. int main(void) {
  15.   LOGINREC *login;
  16.   DBPROCESS *dbconn;
  17.   char hostname[MAXHOSTNAMELEN];
  18.   int max_len = MAXHOSTNAMELEN;
  19.   DBCHAR username[255];
  20.   DBCHAR first_name[255];
  21.   DBCHAR last_name[255];
  22.  
  23.   /* Init the DB library */
  24.   if (dbinit() == FAIL) {
  25.     fprintf(stderr, "Could not init db.\n");
  26.     return 1;
  27.   }
  28.  
  29.   /* Allocate a login params structure */
  30.   if ((login = dblogin()) == FAIL) {
  31.     fprintf(stderr, "Could not initialize dblogin() structure.\n");
  32.     return 2;
  33.   }
  34.  
  35.   /* Initialize the login params in the structure */
  36.   DBSETLUSER(login, UID);
  37.   DBSETLPWD(login, PWD);
  38.   DBSETLAPP(login, PROGNAME);
  39.   if (gethostname(hostname, max_len) == 0)
  40.     DBSETLHOST(login, hostname);
  41.    
  42.  
  43.   /* Now connect to the DB Server */
  44.   if ((dbconn = dbopen(login, DBSERVER)) == NULL) {
  45.     fprintf(stderr, "Could not connect to DB Server: %s\n", DBSERVER);
  46.     return 3;
  47.   }
  48.  
  49.   /* Now switch to the correct database */
  50.   if ((dbuse(dbconn, DBNAME)) == FAIL) {
  51.     fprintf(stderr, "Could not switch to database %s on DB Server %s\n", DBNAME, DBSERVER);
  52.     return 4;
  53.   }
  54.  
  55.   /* You can free the login structure now, as it is no longer needed after logging in */
  56.   dbloginfree(login);
  57.  
  58.   /* Now prepare a SQL statement */
  59.   dbcmd(dbconn, "SELECT username, first_name, last_name FROM admin_users WITH (NOLOCK)");
  60.   /* Can use dbfcmd instead, if you want to format the SQL
  61.      E.g.
  62.      dbfcmd(dbconn, "SELECT id FROM Customer WHERE company_name='%s' AND avg_income > %f",
  63.             company_name, avg_income);
  64.   */
  65.  
  66.   /* Now execute the SQL statement */
  67.   if (dbsqlexec(dbconn) == FAIL) {
  68.     fprintf(stderr, "Could not execute the sql statement\n");
  69.     return 5;
  70.   }
  71.   dbresults(dbconn); /* Very important to call this! :) */
  72.  
  73.   /* Now bind the returned columns to the variables */
  74.   dbbind(dbconn, 1, NTBSTRINGBIND, 0, (BYTE *)&username);
  75.   dbbind(dbconn, 2, NTBSTRINGBIND, 0, (BYTE *)&first_name);
  76.   dbbind(dbconn, 3, NTBSTRINGBIND, 0, (BYTE*)&last_name);
  77.  
  78.   /* Loop thru the result set */
  79.   while (dbnextrow(dbconn) != NO_MORE_ROWS) {
  80.     /* print out the data */
  81.     printf("%s %s %s\n", username, first_name, last_name);
  82.   }
  83.  
  84.   /* Clean up*/
  85.   dbfreebuf(dbconn);
  86.   dbclose(dbconn);
  87.   dbexit();
  88.  
  89.   return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement