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

📄 example10.c

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

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

/*
**    example10.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 insert data from a file
**        into the database. 
**
**        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"

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


main()
{
    DBPROCESS    *q_dbproc;          /* connection with server. */
    DBPROCESS    *u_dbproc;          /* ibid.                   */
    LOGINREC     *login;             /* login information.      */
    char         filename[80];
    char         sqlcmd[SQLBUFLEN];
    char         au_id[12];
    DBBINARY     buf[DBBUFSIZE];     /* DBBINARY so 1st bytes   */
                                     /* come out right...       */
    char         c;
    long         length, num_done;
    FILE         *fp;
    RETCODE      result_code;

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

    /*
    **  First, we need to get two pieces of data
    **  from the user: the au_id and the filename:
    */

    fprintf(OUT_CH, "Please enter an author-id: \n");
    fscanf(stdin,"%s", au_id);
    if ( strlen(au_id) >= 12)
    {
        fprintf(OUT_CH,"Author-id must be less than 12 digits\n");
        exit(ERREXIT);
    }


    fprintf(OUT_CH, "Please enter the name of a ");
    fprintf(OUT_CH, "file that holds an image: \n");
    fscanf(stdin,"%s",filename);

    /* Open the recipient file: */
    if ((fp = fopen(filename, "r")) == (FILE *)NULL)
    {
        fprintf (ERR_CH, "Can't open %s!", filename);
        exit(ERREXIT);
    }

    /* Get length of file to read: */
    length = 0;            
    while(feof(fp) == 0)
    {
	length += fread(buf, sizeof(DBBINARY), DBBUFSIZE, fp);
    }
    rewind(fp);

    /* 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:    */
    login = dblogin();

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


    /*
    ** Get two DBPROCESS structures to talk to the SQL Server.
    ** One is for querying and one for updating.          
    */

    q_dbproc = dbopen(login, NULL);
    u_dbproc = dbopen(login, NULL);
    
    dbuse(q_dbproc,"pubs2");
    dbuse(u_dbproc,"pubs2");


    /*
    **  The database column "pic" is an image column.  
    **  Example 10 must perform the following steps in
    **  order to insert data into this column:
    **
    **   1. INSERT all data into the row except the image.
    **   2. UPDATE the row to create a valid text pointer.
    **   3. SELECT the row to pull the text pointer and
    **      text timestamp into the DBPROCESS.
    **   4. Call dbtxtptr() to retrieve the text pointer 
    **      from the DBPROCESS.
    **   5. Call dbtxtimestamp() to retrieve the text timestamp 
    **      from the DBPROCESS.
    **   6. Write the image value to SQL Server, using dbwritetext().
    */ 

    /*
    **  Step 1:  INSERT the data:
    */

    sprintf(sqlcmd," insert au_pix (au_id) values ('"); 
    strcat(sqlcmd,au_id);
    strcat(sqlcmd, "')");

    dbcmd(q_dbproc, sqlcmd);
    dbsqlexec(q_dbproc);

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

    while (dbresults(q_dbproc) != NO_MORE_RESULTS)
	continue;

    /*
    **  Step 2:  UPDATE the row:
    */

    strcpy(sqlcmd," update au_pix set pic = null \
                           where au_id = '");
    strcat(sqlcmd,au_id);
    strcat(sqlcmd, "'");

    dbcmd(q_dbproc, sqlcmd);
    dbsqlexec(q_dbproc);

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

    while (dbresults(q_dbproc) != NO_MORE_RESULTS)
	continue;

    /*
    **  Step 3:  SELECT the row:
    */

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

    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) 
        {
            /* One row is returned but we are not interested in it: */
            dbcanquery(q_dbproc);
        }
    }

    /*
    **  Steps 4, 5, and 6:  dbtxtptr(), dbtxtimestamp(),
    **  and dbwritetext():
    */

    dbwritetext (u_dbproc, "au_pix.pic", dbtxptr(q_dbproc, 1), 
                 DBTXPLEN, dbtxtimestamp(q_dbproc, 1), 
                 TRUE, (DBINT)length, NULL);

    dbsqlok(u_dbproc);
    dbresults(u_dbproc);

    /*
    **  More Step 6:  dbmoretext() is being used in conjunction
    **  with dbwritetext() to write the image:
    */

    num_done = 0;
    while((length - num_done) >= DBBUFSIZE) 
    {
        if (fread( (char *)buf, sizeof(DBBINARY), DBBUFSIZE, fp)
              != DBBUFSIZE) 
            fprintf(ERR_CH,"Error in reading image after %d characters!",
		length);
        dbmoretext(u_dbproc, DBBUFSIZE, buf);
        num_done += DBBUFSIZE;
    }

    if ((length - num_done) > 0)   
    {
        if (fread(buf, sizeof(DBBINARY), (int)(length - num_done), fp) 
              != length - num_done)
            error("Error in reading image...\n!", (char *)NULL);

        dbmoretext(u_dbproc, (int) (length - num_done), buf);
    }

    dbsqlok(u_dbproc);
    dbresults(u_dbproc);

    dbexit();
}

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;

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