home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a063 / 13.img / SAMPLE / DBLIB / EXAMPLE5.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-13  |  3.5 KB  |  132 lines

  1. /*    example5.c */
  2. /*
  3. ** This example illustrates dbconvert.    It converts a
  4. ** number of constants to strings, a number of strings
  5. ** to numerical or binary quantities, and a number of
  6. ** numerical quantities to other numerical types.
  7. **
  8. */
  9.  
  10. #define DBMSOS2
  11. #include <stdio.h>
  12. #include <sqlfront.h>
  13. #include <sqldb.h>
  14.  
  15. #define STDEXIT 0
  16. #define ARRAY_LEN    20
  17.  
  18. /* Forward declarations of the error handler and message handler.
  19. */
  20. int    err_handler();
  21. int    msg_handler();
  22.  
  23. main(argc, argv)
  24. int    argc;
  25. char    *argv[];
  26. {
  27.     /* These variables 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.  
  35.     /* Install the user-supplied error-handling and message-handling
  36.     * functions. They are defined at the bottom of this source file.
  37.     */
  38.     dberrhandle(err_handler);
  39.     dbmsghandle(msg_handler);
  40.  
  41.     /* Convert numerical and binary constants to strings. */
  42.  
  43.     dbconvert
  44.     ((DBPROCESS *)NULL, SQLBINARY, my_binary_array, (DBINT)8,
  45.             SQLCHAR, my_string,(DBINT)-1);
  46.     printf("Binary constant 0x123456789abcdef0 converted to string");
  47.     printf("\"%s\".\n\n", my_string);
  48.  
  49.     my_flt8 = 55.555;
  50.     dbconvert
  51.     ((DBPROCESS *)NULL, SQLFLT8, (BYTE *) &my_flt8,
  52.             (DBINT)-1, SQLCHAR, my_string, (DBINT)-1);
  53.     printf
  54.     ("Floating-pt constant 55.555 converted to string \"%s\".\n\n",
  55.     my_string);
  56.  
  57.     /* Convert string constants to numerical and binary quantities. */
  58.  
  59.     dbconvert
  60.     ((DBPROCESS *)NULL, SQLCHAR, "123", (DBINT)-1, SQLINT4, (BYTE *)
  61.         &my_int4, (DBINT)-1);
  62.     printf
  63.     ("String constant \"123\" converted to 4-byte integer %ld.\n\n",
  64.     my_int4);
  65.  
  66.     dbconvert
  67.     ((DBPROCESS *)NULL,
  68.     SQLCHAR, "0xfedc", (DBINT)-1, SQLBINARY, my_binary_array,
  69.             (DBINT)ARRAY_LEN);
  70.     printf("String constant \"0xfedc\" converted to binary sequence ");
  71.     printf("%x.\n\n", *((int *)my_binary_array));
  72.  
  73.     dbconvert
  74.     ((DBPROCESS *)NULL, SQLCHAR, "123.456", (DBINT)-1, SQLFLT8,
  75.        (BYTE *) &my_flt8, (DBINT)-1);
  76.     printf("String constant \"123.456\" converted to ");
  77.     printf("floating-pt number %f.\n\n", my_flt8);
  78.  
  79.     /* Convert numerical types to other numerical types. */
  80.  
  81.     my_flt8 = 98.76;
  82.     dbconvert
  83.        ((DBPROCESS *)NULL, SQLFLT8, (BYTE *) &my_flt8, (DBINT)-1, SQLMONEY,
  84.         (BYTE *) &my_money, (DBINT)-1);
  85.     dbconvert
  86.        ((DBPROCESS *)NULL, SQLMONEY, (BYTE *) &my_money, (DBINT)-1, SQLCHAR,
  87.            my_string, (DBINT)-1);
  88.     printf
  89.     ("floating-pt number %f converted to money value %s.\n\n",
  90.     my_flt8, my_string);
  91.  
  92.     dbconvert
  93.        ((DBPROCESS *)NULL, SQLMONEY, (BYTE *) &my_money, (DBINT)-1, SQLFLT8,
  94.            (BYTE *) &my_flt8, (DBINT)-1);
  95.     printf
  96.     ("money value %s converted to floating-pt value %f.\n\n",
  97.     my_string, my_flt8);
  98. }
  99.  
  100. int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
  101. DBPROCESS    *dbproc;
  102. int    severity;
  103. int    dberr;
  104. int    oserr;
  105. char    *dberrstr;
  106. char    *oserrstr;
  107. {
  108.     if ((dbproc == NULL) || (DBDEAD(dbproc)))
  109.         return(INT_EXIT);
  110.     else
  111.     {
  112.         printf("DB-LIBRARY error:\n\t%s\n", dberrstr);
  113.         if (oserr != DBNOERR)
  114.             printf("Operating-system error:\n\t%s\n", oserrstr);
  115.         return(INT_CANCEL);
  116.     }
  117. }
  118.  
  119. int msg_handler(dbproc, msgno, msgstate, severity, msgtext)
  120. DBPROCESS    *dbproc;
  121. DBINT    msgno;
  122. int    msgstate;
  123. int    severity;
  124. char    *msgtext;
  125. {
  126.     printf
  127.     ("SQL Server message %ld, state %d, severity %d:\n\t%s\n",
  128.     msgno, msgstate, severity, msgtext);
  129.     return(0);
  130. }
  131.  
  132.