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

📄 example2.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 */

/*
**	example2.c
**
** This example opens a data file, inserts data from the file
** into a newly created table containing several of the
** SQL Server datatypes, and binds and prints the results.
*/

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

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

#define BUFLEN      2048
#define HEXLEN      510
#define PLEN        25 


main(argc,argv)
int           argc;
char          *argv[];
{

	LOGINREC         *login;
	DBPROCESS        *dbproc;
	RETCODE          return_code;

	DBTINYINT        age;
	DBSMALLINT       userid;
	DBINT            royalty;
	DBCHAR           name[PLEN+1];
	DBBINARY         title_id[PLEN+1];
	DBBIT            us_citizen;
	DBFLT8           account;
	DBCHAR           title[PLEN+1];     /* string    */
	DBCHAR           manager[PLEN+1];   /* ntbstring */
	DBCHAR           id_buffer[HEXLEN+1];

	char             cmdbuf[BUFLEN];
	FILE             *infile;

	printf("Demo of inserting data from a file to a table\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);
	
	/* Allocate and initialize the LOGINREC structure to be used
	 * to open a connection to SQL Server.
	 */

	login = dblogin();
	DBSETLUSER(login, USER);
	DBSETLPWD(login, PASSWORD);
	DBSETLAPP(login, "example2");
	
	dbproc = dbopen(login, NULL);

	printf("Creating the 'test' database.\n");

	dbcmd(dbproc,"create database test ");

	dbsqlexec(dbproc);
	while (dbresults(dbproc) != NO_MORE_RESULTS)
		continue;

	dbuse(dbproc,"test");

	printf("Creating the 'alltypes' table.\n");

	/* Create a table that contains several SQL Server datatypes. */
	dbcmd(dbproc,"create table alltypes ");
	dbcmd(dbproc,"(age tinyint,");
	dbcmd(dbproc,"userid smallint,");
	dbcmd(dbproc,"royalty int,");
	dbcmd(dbproc,"name char(25),");
	dbcmd(dbproc,"title_id varbinary(20),"); 
	dbcmd(dbproc,"us_citizen bit,");
	dbcmd(dbproc,"account float,");
	dbcmd(dbproc,"title varchar(20),");
	dbcmd(dbproc,"manager char(25))");
  

	dbsqlexec(dbproc);
	while (dbresults(dbproc) != NO_MORE_RESULTS)
		continue;

	/* Insert rows of data into the newly created table "alltypes".
	 * We will read in the contents of the file and form an
	 * INSERT statement.
	 */

	if ((infile=fopen("datafile","r")) == NULL)
	{
		dbexit();
		printf("Unable to open file 'datafile'.\n");
		exit(ERREXIT);
	}

	printf("Inserting rows into the 'alltypes' table.\n");

	while ((fgets(cmdbuf,BUFLEN,infile)) != NULL)
	{
		dbfcmd(dbproc,"insert into alltypes \n");
		dbfcmd(dbproc,"values('%s') \n", (char *)cmdbuf);
	}

	dbsqlexec(dbproc);

	/* Process the results of each of the INSERT statements. */

	while ((return_code = dbresults(dbproc)) != NO_MORE_RESULTS)
	{
		if (return_code == FAIL)
			printf("One of the insert statements FAILed.\n");
	}

	printf("Selecting rows from the 'alltypes' table:\n");

	dbcmd(dbproc,"select * from alltypes");
	dbsqlexec(dbproc);

	while ((return_code = dbresults(dbproc)) != NO_MORE_RESULTS)
	{
		if (return_code == SUCCEED)
		{
			dbbind(dbproc, 1, TINYBIND, (DBINT)0, (BYTE *)&age);
			dbbind(dbproc, 2, SMALLBIND, (DBINT)0, (BYTE *)&userid);
			dbbind(dbproc, 3, INTBIND, (DBINT)0, (BYTE *)&royalty);
			dbbind(dbproc, 4, CHARBIND, (DBINT)0, (BYTE *)name);
			dbbind(dbproc, 5, BINARYBIND, (DBINT)0, title_id);
			dbbind(dbproc, 6, BITBIND, (DBINT)0, (BYTE *)&us_citizen);
			dbbind(dbproc, 7, FLT8BIND, (DBINT)0, (BYTE *)&account);
			dbbind(dbproc, 8, STRINGBIND, (DBINT)0, (BYTE *)title);
			dbbind(dbproc, 9, NTBSTRINGBIND, (DBINT)0, (BYTE *)manager);

			/*
			** Initialize null terminator in "name" array,
			** since CHARBIND does not add one.
			*/
			name[PLEN] = '\0';

			while (dbnextrow(dbproc) != NO_MORE_ROWS)
			{
				dbconvert
					(dbproc, SYBBINARY, title_id,
					 dbdatlen(dbproc, 5), SYBCHAR, (BYTE *)id_buffer, (DBINT)-1);
				printf
					("%d  %d  %ld  %s  0x%s\n",
					 age, userid, royalty, name, id_buffer);
				printf
					("%d  %8.2f  %s  %s\n",
					 us_citizen, account, title, manager);
			}
		}
	}

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