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

📄 example4.c

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

/*
** example4.c:      1.1      5/24/93      13:32:31
**  
**  
**  
*/
#if USE_SCCSID
static char Sccsid[] = {"%Z% %M% %I% %G%"};
#endif /* USE_SCCSID */

/*
**	example4.c
**
** This example accesses the data within each row without using dbbind(),
** and illustrates the use of row buffering.
**
** It runs a query, saves all of the returned rows (up to a maximum
** of 1000) using DB-Library row buffering, and allows the user to
** examine data rows at random.
*/

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

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

DBPROCESS            *dbproc;  /* Our connection with SQL Server. */
LOGINREC             *dlogin;   /* Our login information. */

#define TYPELEN 2


main(argc, argv)
int                  argc;
char                 *argv[];
{
	/* Here are the variables which will be used to store the
	 * data being examined.
	 */
	DBCHAR             name[DBMAXNAME+1];/* DBMAXNAME is defined in
	                                      * "sybdb.h" as the maximum
	                                      * length for names of database
	                                      * objects, such as tables,
	                                      * columns, and procedures.
	                                     */
	DBCHAR             type[TYPELEN+1];
	DBINT              id;

	DBCHAR             datebuf[64];
	DBINT              len;
	char               numstring[32];
	int                quitflag = 0;
	RETCODE            row_code;
	int                rownum;

	printf("Demo of a query without using dbbind()\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);

	/*
	** Get a LOGINREC structure and fill it with the necessary
	** login information.
	*/

	dlogin = dblogin();
	DBSETLUSER(dlogin, USER);
	DBSETLPWD(dlogin, PASSWORD);
	DBSETLAPP(dlogin, "example4");

	dbproc = dbopen(dlogin, NULL);

	dbcmd(dbproc, "select name, type, id, crdate from sysobjects");
	dbcmd(dbproc, " where type = 'S'");

	/*
	** Buffer the rows so we can read them any number of times.
	** Passing '0' as the number of rows to buffer will get us
	** default row buffering (currently 1000 rows).
	** If more than 1000 rows are received, this program will
	** only save the last 1000.
	**
	** Note that this parameter must be passed as an ASCII string.
	*/

	dbsetopt(dbproc, DBBUFFER, "0", -1);

	dbsqlexec(dbproc);

	if (dbresults(dbproc) == SUCCEED) 
	{
		/* Read all of the rows into DB-Library's buffer */

		while ((row_code = dbnextrow(dbproc)) != NO_MORE_ROWS)
		{
			/* If DB-Library's row buffer is full, throw
			 * away the oldest row, to allow the newest
			 * row to be read in.
			 */
			if (row_code == BUF_FULL)
				dbclrbuf(dbproc, 1);
		}
	
		/* Let the user view any row in the table. */

		printf("Type the number of the row you want to see.\n");
		printf("The first row is number 1.\n");
		printf("Asking for row 0 will terminate the program.\n");

		while (quitflag == 0)
		{
			printf("Row number: ");
			gets(numstring);
			rownum = atoi(numstring);

			if (rownum == 0)
				quitflag = 1;
			else
			{
				/* Print out the requested row. */

				if (dbgetrow(dbproc, rownum) == NO_MORE_ROWS)
					printf
						("That row is not in the table.\n");
				else
				{
					/* Copy variable-length character data
					 * (colname).
					 */
		
					strncpy
						(name, (DBCHAR *)dbdata(dbproc, 1),
						 (len = dbdatlen(dbproc, 1)));

					/* String needs terminating null. */

					name[len] = '\0';

					/* Copy fixed-length character data. */

					strncpy
						(type, (DBCHAR *)dbdata(dbproc, 2),
						 (len = dbdatlen(dbproc, 2)));
					type[len] = '\0';

					/* Copy integer data. */

					id = *((DBINT *)dbdata(dbproc, 3));

					/* Print out the column headers. */

					printf
						(" %-20s %10s %30s %15s \n",
			 			"       NAME         ",
			 			"TYPE",
			 			"             DATE             ",
			 			"ID");
					printf
						(" %20s %10s %30s %15s \n",
			 			"--------------------",
			 			"----",
			 			"---------------------------",
			 			"----");

					/* Convert datetime data to a printable
					 * string.
					 */

					dbconvert(dbproc, SYBDATETIME, (dbdata(dbproc, 4)), 
						  (DBINT)-1, SYBCHAR, (BYTE *)datebuf, (DBINT)-1);
					printf
						("%20s %10s %30s %15ld \n",
						 name, type, datebuf, id);
				}
			}
		}

	}

	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 + -