⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example5.c

📁 在SCO UNIX5.05
💻 C
字号:
/*
** Confidential property of Sybase, Inc.
** (c) Copyright Sybase, Inc. 1992 to ???
** All rights reserved.
*/

/*
** %M%:      %I%      %G%      %U%
**  
**  
**  
*/
#if USE_SCCSID
static char Sccsid[] = {"%Z% %M% %I% %G%"};
#endif /* USE_SCCSID */

/*
**	example5.c
**
** This example illustrates dbconvert().  It converts a 
** number of constants to strings, a number of strings 
** to numerical or binary quantities, and a number of
** numerical quantities to other numerical types.
**
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <sybfront.h>
#include <sybdb.h>
#include "sybdbex.h"

#define ARRAY_LEN      20


main(argc, argv)
int           argc;
char          *argv[];
{
	/* These variables will hold the results of data conversions. */
	static DBBINARY   my_binary_array[ARRAY_LEN]
	                   = {0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0};
	DBFLT8            my_flt8;
	DBINT             my_int4;
	DBMONEY           my_money;
	DBCHAR            my_string[ARRAY_LEN];
	DBDATETIME        my_date;

	printf("Demo of the use of dbconvert()\n\n");
	fflush(stdout);

	/* Initialize DB-Library. */
	if (dbinit() == FAIL)
		exit(ERREXIT);

	/* Install the user-supplied error-handling and message-handling
	 * routines. They are defined at the bottom of this source file.
	 */
	dberrhandle(err_handler);
	dbmsghandle(msg_handler);

	/* Convert numerical and binary constants to strings. */

	dbconvert
		((DBPROCESS *)NULL, SYBBINARY, my_binary_array,
		 (DBINT)8, SYBCHAR, (BYTE *)my_string, (DBINT)-1);
	printf("Binary constant 0x123456789abcdef0 converted to string "); 
	printf("\"%s\".\n\n", my_string);

	my_flt8 = 55.555;
	dbconvert
	 ((DBPROCESS *)NULL, SYBFLT8, (BYTE *)&my_flt8,
	  (DBINT)-1, SYBCHAR, (BYTE *)my_string, (DBINT)-1);
	printf
	 ("Floating-pt constant 55.555 converted to string \"%s\".\n\n",
	  my_string);

	/* Convert string constants to numerical and binary quantities. */

	dbconvert
		((DBPROCESS *)NULL, SYBCHAR, (BYTE *)"123", (DBINT)-1,
		 SYBINT4, (BYTE *)&my_int4, (DBINT)-1);
	printf
		("String constant \"123\" converted to 4-byte integer %ld.\n\n",
		 my_int4);

	dbconvert
		((DBPROCESS *)NULL, SYBCHAR, (BYTE *)"0xfedc", (DBINT)-1, SYBBINARY, 
		 (BYTE *)my_binary_array, (DBINT)ARRAY_LEN);
	printf("String constant \"0xfedc\" converted to binary sequence ");
	printf("%x.\n\n", *((int *)my_binary_array));

	dbconvert
		((DBPROCESS *)NULL, SYBCHAR, (BYTE *)"123.456", (DBINT)-1, SYBFLT8, 
		 (BYTE *)&my_flt8, (DBINT)-1);
	printf("String constant \"123.456\" converted to ");
	printf("floating-pt number %f.\n\n", my_flt8);

	/* Convert numerical types to other numerical types. */

	my_flt8 = 98.76;
	dbconvert
		((DBPROCESS *)NULL, SYBFLT8, (BYTE *)&my_flt8, (DBINT)-1, SYBMONEY, 
		 (BYTE *)&my_money, (DBINT)-1);
	dbconvert
		((DBPROCESS *)NULL, SYBMONEY, (BYTE *)&my_money, (DBINT)-1, SYBCHAR, 
		 (BYTE *)my_string, (DBINT)-1);
	printf
		("floating-pt number %f converted to money value %s.\n\n",
		 my_flt8, my_string);

	dbconvert
		((DBPROCESS *)NULL, SYBMONEY, (BYTE *)&my_money, (DBINT)-1, SYBFLT8, 
		 (BYTE *)&my_flt8, (DBINT)-1);
	printf
		("money value %s converted to floating-pt value %f.\n\n",
		 my_string, my_flt8);

	/*
	**  Datetime conversions:  
	*/

        /*
        **  Many formats are acceptable for dates.  For a list
        **  of these formats, see the Commands Reference
        **  datatypes page.
        */

	dbconvert
		((DBPROCESS *)NULL, SYBCHAR, (BYTE *)"November 11, 1955", 
		 (DBINT)-1, SYBDATETIME, (BYTE *)&my_date , (DBINT)-1);
	dbconvert
		((DBPROCESS *)NULL, SYBMONEY, (BYTE *)&my_date, 
                 (DBINT)-1, SYBCHAR, (BYTE *)my_string, (DBINT)-1);

	printf ("Internal date representation converted ");
        printf  ("to string value %s.\n\n", my_string);

	dbdatename((DBPROCESS *)NULL, my_string, DBDATE_DY, &my_date);

	printf ("Day-of-year extracted from internal representation:"); 
        printf (" %s.\n\n", my_string);

	dbdatename((DBPROCESS *)NULL, my_string, DBDATE_DW, &my_date);

	printf ("Day-of-week extracted from internal representation:"); 
        printf (" %s.\n\n", my_string);

	dbexit();
	exit(STDEXIT);
}

int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
DBPROCESS    *dbproc;
int          severity;
int          dberr;
int          oserr;
char         *dberrstr;
char         *oserrstr;
{
    if ((dbproc == (DBPROCESS *)NULL) || (DBDEAD(dbproc)))
        return(INT_EXIT);
    else 
    {
        fprintf (ERR_CH, "DB-Library error:\n\t%s\n", dberrstr);

        if (oserr != DBNOERR)
            fprintf (ERR_CH, "Operating-system error:\n\t%s\n", oserrstr);

        return(INT_CANCEL);
    }
}

int msg_handler(dbproc, msgno, msgstate, severity, msgtext, 
                srvname, procname, line)

DBPROCESS       *dbproc;
DBINT           msgno;
int             msgstate;
int             severity;
char            *msgtext;
char            *srvname;
char            *procname;
DBUSMALLINT     line;

{
	fprintf (ERR_CH, "Msg %ld, Level %d, State %d\n", 
	        msgno, severity, msgstate);

	if (strlen(srvname) > 0)
		fprintf (ERR_CH, "Server '%s', ", srvname);
	if (strlen(procname) > 0)
		fprintf (ERR_CH, "Procedure '%s', ", procname);
	if (line > 0)
		fprintf (ERR_CH, "Line %d", line);

	fprintf (ERR_CH, "\n\t%s\n", msgtext);

	return(0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -