home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a063 / 9.img / SAMPLE / DBLIB / EXAMPLE5.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-02  |  4.4 KB  |  163 lines

  1. /*
  2. **    example5.c
  3. **
  4. ** This example illustrates dbconvert().  It converts a 
  5. ** number of constants to strings, a number of strings 
  6. ** to numerical or binary quantities, and a number of
  7. ** numerical quantities to other numerical types.
  8. **
  9. */
  10.  
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <sybfront.h>
  15. #include <sybdb.h>
  16.  
  17. #define ARRAY_LEN      20
  18.  
  19. /* Function Prototypes */
  20. int             err_handler(DBPROCESS *, int, int, int, char *, char *);
  21. int             msg_handler(DBPROCESS *, DBINT, int, int, char *, char *, 
  22.                                         char *, DBUSMALLINT);
  23. int                  main();
  24.  
  25. main()
  26. {
  27.     /* These variables will hold the results of data conversions. */
  28.     static DBBINARY   my_binary_array[ARRAY_LEN]
  29.                        = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0};
  30.     DBFLT8            my_flt8;
  31.     DBINT             my_int4;
  32.     DBMONEY           my_money;
  33.     DBCHAR            my_string[ARRAY_LEN];
  34.     DBCHAR                *temp_test = "Jan 12, 1990 12:12:12:123AM";
  35.     DBDATETIME            temp_testdt;
  36.  
  37.     /* Initialize DB-Library. */
  38.     if (dbinit() == FAIL)
  39.         exit(ERREXIT);
  40.  
  41.     /* Install the user-supplied error-handling and message-handling
  42.      * routines. They are defined at the bottom of this source file.
  43.      */
  44.     dberrhandle(err_handler);
  45.     dbmsghandle(msg_handler);
  46.  
  47.     /* Convert numerical and binary constants to strings. */
  48.  
  49.     dbconvert
  50.         ((DBPROCESS *)NULL,
  51.          SYBBINARY, my_binary_array, (DBINT)8, SYBCHAR, my_string, (DBINT)-1);
  52.     printf("Binary constant 0x123456789abcdef0 converted to string "); 
  53.     printf("\"%s\".\n\n", my_string);
  54.  
  55.     my_flt8 = 55.555;
  56.     dbconvert
  57.      ((DBPROCESS *)NULL, SYBFLT8, &my_flt8, (DBINT)-1, SYBCHAR, my_string, 
  58.       (DBINT)-1);
  59.     printf
  60.      ("Floating-pt constant 55.555 converted to string \"%s\".\n\n",
  61.       my_string);
  62.  
  63.     /* Convert string constants to numerical and binary quantities. */
  64.  
  65.     dbconvert
  66.         ((DBPROCESS *)NULL, SYBCHAR, "123", (DBINT)-1, SYBINT4, &my_int4, 
  67.          (DBINT)-1);
  68.     printf
  69.         ("String constant \"123\" converted to 4-byte integer %ld.\n\n",
  70.          my_int4);
  71.  
  72.     dbconvert
  73.         ((DBPROCESS *)NULL, SYBCHAR, "0xfedc", (DBINT)-1, SYBBINARY, 
  74.          my_binary_array, (DBINT)ARRAY_LEN);
  75.     printf("String constant \"0xfedc\" converted to binary sequence ");
  76.     printf("%x.\n\n", *((int *)my_binary_array));
  77.  
  78.     dbconvert
  79.         ((DBPROCESS *)NULL, SYBCHAR, "123.456", (DBINT)-1, SYBFLT8, 
  80.          &my_flt8, (DBINT)-1);
  81.     printf("String constant \"123.456\" converted to ");
  82.     printf("floating-pt number %f.\n\n", my_flt8);
  83.  
  84.     /* Convert numerical types to other numerical types. */
  85.  
  86.     my_flt8 = 98.76;
  87.     dbconvert
  88.         ((DBPROCESS *)NULL, SYBFLT8, &my_flt8, (DBINT)-1, SYBMONEY, 
  89.          &my_money, (DBINT)-1);
  90.     dbconvert
  91.         ((DBPROCESS *)NULL, SYBMONEY, &my_money, (DBINT)-1, SYBCHAR, 
  92.          my_string, (DBINT)-1);
  93.     printf
  94.         ("floating-pt number %f converted to money value %s.\n\n",
  95.          my_flt8, my_string);
  96.  
  97.     dbconvert
  98.         ((DBPROCESS *)NULL, SYBMONEY, &my_money, (DBINT)-1, SYBFLT8, 
  99.          &my_flt8, (DBINT)-1);
  100.     printf
  101.         ("money value %s converted to floating-pt value %f.\n\n",
  102.          my_string, my_flt8);
  103.  
  104. /* DATETIME conversion tests */
  105.     dbconvert((DBPROCESS *)NULL, SYBCHAR, temp_test, (DBINT)-1, SYBDATETIME,
  106.         &temp_testdt, (DBINT)-1);
  107.     dbconvert((DBPROCESS *)NULL, SYBDATETIME, &temp_testdt, (DBINT)-1, SYBCHAR,
  108.         temp_test, (DBINT)-1);
  109.     printf("date: %s\n",temp_test);
  110.  
  111.     exit(STDEXIT);
  112.     dbexit();
  113. }
  114.  
  115. int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
  116. DBPROCESS       *dbproc;
  117. int             severity;
  118. int             dberr;
  119. int             oserr;
  120. char            *dberrstr;
  121. char            *oserrstr;
  122. {
  123.     if ((dbproc == NULL) || (DBDEAD(dbproc)))
  124.         return(INT_EXIT);
  125.     else 
  126.     {
  127.         printf("DB-Library error:\n\t%s\n", dberrstr);
  128.  
  129.         if (oserr != DBNOERR)
  130.             printf("Operating-system error:\n\t%s\n", oserrstr);
  131.  
  132.         return(INT_CANCEL);
  133.     }
  134. }
  135.  
  136. int msg_handler(dbproc, msgno, msgstate, severity, msgtext, 
  137.                 srvname, procname, line)
  138.  
  139. DBPROCESS       *dbproc;
  140. DBINT           msgno;
  141. int             msgstate;
  142. int             severity;
  143. char            *msgtext;
  144. char            *srvname;
  145. char            *procname;
  146. DBUSMALLINT     line;
  147.  
  148. {
  149.     printf ("Msg %ld, Level %d, State %d\n", 
  150.             msgno, severity, msgstate);
  151.  
  152.     if (strlen(srvname) > 0)
  153.         printf ("Server '%s', ", srvname);
  154.     if (strlen(procname) > 0)
  155.         printf ("Procedure '%s', ", procname);
  156.     if (line > 0)
  157.         printf ("Line %d", line);
  158.  
  159.     printf("\n\t%s\n", msgtext);
  160.  
  161.     return(0);
  162. }
  163.