home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 5 / 05.iso / a / a025 / 11.ddi / EXAMPLE5.C@ / EXAMPLE5.bin
Encoding:
Text File  |  1992-09-15  |  3.4 KB  |  131 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 ARRAY_LEN    20
  16.  
  17. /* Forward declarations of the error handler and message handler.
  18. */
  19. int    err_handler();
  20. int    msg_handler();
  21.  
  22. main(argc, argv)
  23. int    argc;
  24. char    *argv[];
  25. {
  26.     /* These variables hold the results of data conversions. */
  27.     static DBBINARY    my_binary_array[ARRAY_LEN]
  28.     = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0};
  29.     DBFLT8    my_flt8;
  30.     DBINT    my_int4;
  31.     DBMONEY    my_money;
  32.     DBCHAR    my_string[ARRAY_LEN];
  33.  
  34.     /* Install the user-supplied error-handling and message-handling
  35.     * functions. They are defined at the bottom of this source file.
  36.     */
  37.     dberrhandle(err_handler);
  38.     dbmsghandle(msg_handler);
  39.  
  40.     /* Convert numerical and binary constants to strings. */
  41.  
  42.     dbconvert
  43.     ((DBPROCESS *)NULL, SQLBINARY, my_binary_array, (DBINT)8,
  44.             SQLCHAR, my_string,(DBINT)-1);
  45.     printf("Binary constant 0x123456789abcdef0 converted to string");
  46.     printf("\"%s\".\n\n", my_string);
  47.  
  48.     my_flt8 = 55.555;
  49.     dbconvert
  50.     ((DBPROCESS *)NULL, SQLFLT8, (BYTE *) &my_flt8,
  51.             (DBINT)-1, SQLCHAR, my_string, (DBINT)-1);
  52.     printf
  53.     ("Floating-pt constant 55.555 converted to string \"%s\".\n\n",
  54.     my_string);
  55.  
  56.     /* Convert string constants to numerical and binary quantities. */
  57.  
  58.     dbconvert
  59.     ((DBPROCESS *)NULL, SQLCHAR, "123", (DBINT)-1, SQLINT4, (BYTE *)
  60.         &my_int4, (DBINT)-1);
  61.     printf
  62.     ("String constant \"123\" converted to 4-byte integer %ld.\n\n",
  63.     my_int4);
  64.  
  65.     dbconvert
  66.     ((DBPROCESS *)NULL,
  67.     SQLCHAR, "0xfedc", (DBINT)-1, SQLBINARY, my_binary_array,
  68.             (DBINT)ARRAY_LEN);
  69.     printf("String constant \"0xfedc\" converted to binary sequence ");
  70.     printf("%x.\n\n", *((int *)my_binary_array));
  71.  
  72.     dbconvert
  73.     ((DBPROCESS *)NULL, SQLCHAR, "123.456", (DBINT)-1, SQLFLT8,
  74.        (BYTE *) &my_flt8, (DBINT)-1);
  75.     printf("String constant \"123.456\" converted to ");
  76.     printf("floating-pt number %f.\n\n", my_flt8);
  77.  
  78.     /* Convert numerical types to other numerical types. */
  79.  
  80.     my_flt8 = 98.76;
  81.     dbconvert
  82.        ((DBPROCESS *)NULL, SQLFLT8, (BYTE *) &my_flt8, (DBINT)-1, SQLMONEY,
  83.         (BYTE *) &my_money, (DBINT)-1);
  84.     dbconvert
  85.        ((DBPROCESS *)NULL, SQLMONEY, (BYTE *) &my_money, (DBINT)-1, SQLCHAR,
  86.            my_string, (DBINT)-1);
  87.     printf
  88.     ("floating-pt number %f converted to money value %s.\n\n",
  89.     my_flt8, my_string);
  90.  
  91.     dbconvert
  92.        ((DBPROCESS *)NULL, SQLMONEY, (BYTE *) &my_money, (DBINT)-1, SQLFLT8,
  93.            (BYTE *) &my_flt8, (DBINT)-1);
  94.     printf
  95.     ("money value %s converted to floating-pt value %f.\n\n",
  96.     my_string, my_flt8);
  97. }
  98.  
  99. int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
  100. DBPROCESS    *dbproc;
  101. int    severity;
  102. int    dberr;
  103. int    oserr;
  104. char    *dberrstr;
  105. char    *oserrstr;
  106. {
  107.     if ((dbproc == NULL) || (DBDEAD(dbproc)))
  108.         return(INT_EXIT);
  109.     else
  110.     {
  111.         printf("DB-LIBRARY error:\n\t%s\n", dberrstr);
  112.         if (oserr != DBNOERR)
  113.             printf("Operating-system error:\n\t%s\n", oserrstr);
  114.         return(INT_CANCEL);
  115.     }
  116. }
  117.  
  118. int msg_handler(dbproc, msgno, msgstate, severity, msgtext)
  119. DBPROCESS    *dbproc;
  120. DBINT    msgno;
  121. int    msgstate;
  122. int    severity;
  123. char    *msgtext;
  124. {
  125.     printf
  126.     ("SQL Server message %ld, state %d, severity %d:\n\t%s\n",
  127.     msgno, msgstate, severity, msgtext);
  128.     return(0);
  129. }
  130.  
  131.