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

📄 example11.c

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

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

/*
**    example11.c
**
**        This example uses the au_pix table in the pubs2 database
**        to show the basic outline of how to use the image
**        routines in DB-Library to retrieve data from a table
**        in the database and put the result into a file.
**
**        The format of the au_pix table is:
**
**        Column          Datatype      Size      Nulls Allowed?
**        =============== ============= ========= ==============
**        au_id           char          11           NO  
**        pic             image                      YES
**        format_type     char          11           YES 
**        bytesize        int                        YES
**        pixwidth_hor    char          14           YES 
**        pixwidth_vert   char          14           YES 
**
*/



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

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

#define  IMAGESIZE "60000"

/* Forward reference
*/
void error PROTOTYPE((char *, char *));       /* print error message and die */


main()
{
    DBPROCESS    *q_dbproc;            /* connection with server. */
    LOGINREC     *login;               /* login information.      */
    char         sqlcmd[255];
    DBBINARY     *image;
    char         au_id[15];
    char         filename[80];
    long         length, n_written;
    FILE         *fp;
    RETCODE      result_code;

	printf("Demo of the use the image routines to retrieve data\n\n");
	fflush(stdout);

    /* Start by creating the recipient file */
    
    fprintf(OUT_CH, "Please enter the author-id ");
    fprintf(OUT_CH, "to retrieve: \n");
    fscanf(stdin,"%s", au_id);
    if ( strlen(au_id) >= 15)
    {
        fprintf(OUT_CH,"Author-id must be less than 15 digits\n");
        exit(ERREXIT);
    }

    fprintf(OUT_CH, "Please enter the filename that will ");
    fprintf(OUT_CH, "hold the image: \n");
    fscanf(stdin,"%s",filename);


    if ((fp = fopen(filename, "wb")) == (FILE*)NULL)
    {
        fprintf (ERR_CH,"Can't create %s\n", filename);
        exit(ERREXIT);
    }

    /* Initialize DB-Library:  */
    if (dbinit() == FAIL)
    {
        fprintf (ERR_CH,"Can't initialize dblib...\n");
        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:  */
    login = dblogin();

    DBSETLUSER(login,USER);
    DBSETLPWD(login, PASSWORD);

    /*
    **  Get a DBPROCESS structure to talk to SQL Server:
    */
    
    q_dbproc = dbopen(login, NULL);
    
    /*
    **  The database column "pic" is an image column.    
    **  Retrieve the value of one of its rows:
    */
      
    dbuse(q_dbproc,"pubs2");

    /* Set @@textsize to 60000 so it'll hold our image: */
    dbsetopt(q_dbproc, DBTEXTSIZE, IMAGESIZE, -1);

    /* Get a pointer to the image: */

    strcpy(sqlcmd," select pic from au_pix where au_id = '");

    /* lint complains:  strcpy:  value declared inconsistently */
    strcat(sqlcmd,au_id);
    strcat(sqlcmd,"'");

    /* lint complains:  strcat:  value declared inconsistently */
    dbcmd(q_dbproc, sqlcmd);
    dbsqlexec(q_dbproc);

    /*
    **  It is good programming practice to always call dbresults
    **  in a loop:
    */

    while ((result_code = dbresults(q_dbproc)) != NO_MORE_RESULTS)
    {
        if (result_code == SUCCEED) 
        {
            /* Write the image to the file: */

            n_written = 0;
            while (dbnextrow(q_dbproc) != NO_MORE_ROWS) 
            {
                length = dbdatlen(q_dbproc, 1);
                image = (DBBINARY *)dbdata(q_dbproc,1);

                while (length - n_written >= DBBUFSIZE)
                {
                    if (fwrite(image + n_written,
                        sizeof(DBBINARY), (int)DBBUFSIZE, fp) 
                        != DBBUFSIZE)
                      error("Error in writing image!\n", (char *)NULL);

                    n_written += DBBUFSIZE;
                }

                if (length - n_written >= 0)
                {
                    if (fwrite(image + n_written, 
                        sizeof(DBBINARY), length - n_written, fp) 
                        != length - n_written)
                        error("Error in writing final image!\n", 
                               (char *)NULL);
                }
            }    
        }
    }
    dbexit();
    exit(0);
}

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;

{
    if (msgno != 5701) 
    {
        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);
}

void error(s1, s2)        /* print error message and die */
char *s1, *s2;
{
    fprintf (ERR_CH, s1, s2);
    fprintf (ERR_CH, "\n");
    dbexit();
    exit(ERREXIT);
}

⌨️ 快捷键说明

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