📄 example9.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 */
/*
** example9.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.
**
** 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 <stdlib.h>
#include <sybfront.h>
#include <sybdb.h>
#include "sybdbex.h"
#define IMAGE_LENGTH ((long)301)
#define AU_ID ((char*)"000-00-0000")
/* Forward declarations of the error handler and message handler. */
void doexit PROTOTYPE((int value));
void getimage PROTOTYPE((char *ptr));
int compare PROTOTYPE((char *src1, char *src2, int length));
main()
{
DBPROCESS *q_dbproc; /* query connection with SQL Server */
DBPROCESS *u_dbproc; /* update connection with SQL Server */
LOGINREC *login; /* login information. */
char data[IMAGE_LENGTH];
RETCODE result_code;
int compare();
/*
** Here are the variables which will be used to store the
** data being examined:
**
*/
void getimage();
BYTE *ret_p; /* to hold image pointer */
DBBINARY *txptr; /* text pointer */
DBBINARY *ts_ptr; /* time stamp */
printf("Demo of the image routines in DB-Library\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.
*/
login = dblogin();
DBSETLUSER(login,USER); /* defined in sybdbex.h */
DBSETLPWD(login,PASSWORD);
DBSETLAPP(login, "Example 9");
q_dbproc = dbopen(login, NULL);
u_dbproc = dbopen(login, NULL);
/* Send ``use database'' commands: */
dbuse(q_dbproc,"pubs2");
dbuse(u_dbproc,"pubs2");
/*
** Get the image for insertion
*/
getimage(data);
/*
** The database column "pic" is an image column.
** Example 9 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:
*/
dbfcmd(q_dbproc, "insert au_pix (au_id) values ('%s')",AU_ID);
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:
*/
dbfcmd(q_dbproc, "update au_pix set pic = null \
where au_id = '%s'",AU_ID);
dbsqlexec(q_dbproc);
/*
** It is good programming practice to call always call dbresults
** in a loop.
*/
while (dbresults(q_dbproc) != NO_MORE_RESULTS)
continue;
/*
** Step 3: SELECT the row.
*/
dbfcmd(q_dbproc, "select pic from au_pix \
where au_id = '%s'",AU_ID);
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);
}
}
/*
** Step 4: dbtxtptr():
*/
/* Get the text pointer: */
txptr = dbtxptr(q_dbproc, 1);
/*
** Step 5: dbtxtimestamp():
*/
/* Get a pointer to the text timestamp */
ts_ptr = dbtxtimestamp(q_dbproc, 1);
/*
** Step 6: dbwritetext():
*/
/* Write the image: */
dbwritetext (u_dbproc, "au_pix.pic", txptr, DBTXPLEN,
ts_ptr, TRUE, IMAGE_LENGTH, (BYTE *)data);
/*
** Image data has been inserted in to the server
** and we are about to select it out for compare
** to see if it got there OK.
*/
dbfcmd(q_dbproc, "select pic from au_pix \
where au_id = '%s'",AU_ID);
dbsqlexec(q_dbproc);
/*
** It is good programming practice to call dbresults in a loop
** even if only a single set of results is expected:
*/
while ((result_code = dbresults(q_dbproc)) != NO_MORE_RESULTS)
{
if (result_code == SUCCEED)
{
/* We are expecting only a single row: */
while ( dbnextrow(q_dbproc) != NO_MORE_ROWS)
{
ret_p = dbdata(q_dbproc,1);
}
}
}
/* Compare the two images for symmetry: */
if (compare ((char *)ret_p, data, (int)IMAGE_LENGTH) != 0)
{
fprintf (ERR_CH,"\n Image data not symmetric, exiting.");
doexit(ERREXIT);
}
else
fprintf(OUT_CH,"\n Image data symmetric, everything OK.");
fprintf(OUT_CH,"\nBYE.\n");
/* Now we clean up: */
dbfcmd(u_dbproc, "delete au_pix where au_id = '%s'",AU_ID);
dbsqlexec(u_dbproc);
/*
** It is good programming practice to call dbresults even if
** no results are expected:
*/
while ((result_code = dbresults(u_dbproc)) != NO_MORE_RESULTS)
{
if (result_code == SUCCEED)
{
/*
** No return rows are expected, but it is good
** programming practice to call dbcanquery, just
** in case someone tinkers with the command batch.
*/
dbcanquery(u_dbproc);
}
}
doexit(STDEXIT);
}
void
doexit(value)
int value;
{
dbexit(); /* always call dbexit before returning to OS */
exit(value);
}
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) && (msgno != 5703))
{
fprintf (ERR_CH,"Msg %ld, Level %d, State %d\n",
msgno, severity, msgstate);
fprintf (ERR_CH,"\n\t%s\n", msgtext);
return(0);
}
}
/* generate some random information
** to put in the image row
*/
void
getimage(ptr)
char *ptr;
{
long loop;
/*
** fill the buffer with ordered data
*/
for( loop=0;loop<IMAGE_LENGTH;loop++ )
{
ptr[loop] = (loop % 255); /* keep in range of single byte */
}
return;
}
/*
** A function to compare two arrays of data,
** equivalent to bcmp or memcmp,
** used in place of either of these to be system independent.
*/
int
compare(src1,src2,length)
char *src1,*src2;
int length;
{
for (; length > 0; length--, ++src1, ++src2)
{
if ( (src1 == NULL) || (src2 == NULL) )
return(-1);
if (*src1 != *src2)
{
return(*src1 < *src2 ? -1 : 1);
}
}
return(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -