mult_text.c

来自「sybase linux or unix develope library pa」· C语言 代码 · 共 642 行 · 第 1/2 页

C
642
字号
		{			printf("ct_fetch returned row fail \n");			continue ;		}				/* Get the text data item in the second column. Loop until		** we have all the data for this column. Display the data and 		** then call the update routine.		** Repeat for other text/image columns.		*/		txtptr = textdata->textbuf;		textdata->textlen = 0;		do		{		        retcode = ct_get_data(cmd_ptr, 2, txtptr, 5, &len);                        textdata->textlen += len;                        /*                        ** Protect against overflowing the string buffer.                        */                        if ((textdata->textlen + 5) > (254 ))                        {                                break;                        }                        txtptr += len;                } while (retcode == CS_SUCCEED);		printf("Length of first text column = %d\n",textdata->textlen);	                if (retcode != CS_END_ITEM)                {                        printf(" ct_get_data(2) failed");                        return retcode;                }		retcode = ct_data_info(cmd_ptr, CS_GET, 2, &textdata->iodesc);			RETURN_IF(retcode, "ct_data_info 1 ");		DisplayData(textdata);		printf("Updating first text column .. \n");		retcode = updatetextdata(textdata,EX_TXT_UPD1_VALUE);			RETURN_IF(retcode, "updatetextdata 1 ");		printf("Successfully updated first text column! \n");		/* On to the next text column. */		txtptr2 = textdata2->textbuf;		textdata2->textlen = 0;		do		{		        retcode = ct_get_data(cmd_ptr, 3, txtptr2, 5, &len2);                        textdata2->textlen += len2;                        /*                        ** Protect against overflowing the string buffer.                        */                        if ((textdata2->textlen + 5) > (254 ))                        {                                break;                        }                        txtptr2 += len2;                } while (retcode == CS_SUCCEED);		printf("Length of 2nd text column = %d\n",textdata2->textlen);	                if (retcode != CS_END_DATA)                {                        printf(" ct_get_data(3) failed");                        return retcode;                }		retcode = ct_data_info(cmd_ptr, CS_GET, 3, &textdata2->iodesc);			RETURN_IF(retcode, "ct_data_info 2 ");		DisplayData(textdata2);		printf("Updating second text column .. \n");		retcode = updatetextdata(textdata2,EX_TXT_UPD2_VALUE);			RETURN_IF(retcode, "updatetextdata 2 ");		printf("Successfully updated second text column! \n");	}	printf("\n");		return CS_SUCCEED ;}CS_VOID DisplayData(textdata)TEXT_DATA       *textdata;{        char buf[256];         /*        ** Add a null terminator at the end of the        ** text for displaying the value.        */        memcpy(buf, textdata->textbuf, textdata->textlen);        buf[textdata->textlen] = '\0';        fprintf(stdout, "\n\n The text data retrieved is: \t%s\n", buf);        fflush(stdout);}CS_RETCODE updatetextdata( textdata, newdata)TEXT_DATA	*textdata;char		*newdata;{   	CS_RETCODE      retcode;        CS_INT          res_type;        CS_COMMAND      *cmd;        CS_INT          i;        CS_TEXT         *txtptr;        CS_INT          txtlen;	/*        ** Allocate a command handle to send the text with        */        if ((retcode = ct_cmd_alloc(conn_ptr2, &cmd)) != CS_SUCCEED)        {                printf("UpdateTextData: ct_cmd_alloc() failed");                return retcode;        }         /*        ** Inform Client-Library the next data sent will be        ** used for a text or image update.        */        if ((retcode = ct_command(cmd, CS_SEND_DATA_CMD, NULL, CS_UNUSED,                        CS_COLUMN_DATA)) != CS_SUCCEED)        {                printf("UpdateTextData: ct_command() failed");                return retcode;        }         /*        ** Fill in the description information for the update        ** and send it to Client-Library.        */        txtptr = (CS_TEXT *)newdata;  	txtlen = strlen(newdata);         textdata->iodesc.total_txtlen = txtlen;        textdata->iodesc.log_on_update = CS_TRUE;        retcode = ct_data_info(cmd, CS_SET, CS_UNUSED, &textdata->iodesc);        if (retcode != CS_SUCCEED)        {                printf("UpdateTextData: ct_data_info() failed");                return retcode;        }         /*        ** Send the text one byte at a time. This is not the best thing to do        ** for performance reasons, but does demonstrate the ct_send_data()        ** can handle arbitrary amounts of data.        */        for (i = 0; i < txtlen; i++, txtptr++)        {                retcode = ct_send_data(cmd, txtptr, (CS_INT)1);                if (retcode != CS_SUCCEED)                {                        printf("UpdateTextData: ct_send_data() failed");                        return retcode;                }        } 	/*        ** ct_send_data() does writes to internal network buffers. To insure        ** that all the data is flushed to the server, a ct_send() is done.        */        retcode = ct_send(cmd);                RETURN_IF(retcode, "UpdateTextData: ct_send() ");         /*        ** Process the results of the command        */        while ((retcode = ct_results(cmd, &res_type)) == CS_SUCCEED)        {                switch ((int)res_type)                {                    case CS_PARAM_RESULT:                        /*                        ** Retrieve a description of the parameter data.                        ** Only timestamp data is expected in this example.                        */                        retcode = ProcessTimestamp(cmd, textdata);                        if (retcode != CS_SUCCEED)			{                         printf("UpdateTextData: ProcessTimestamp() failed") ;                                /*                                ** Something failed so cancel all results.                                */                                ct_cancel(NULL, cmd, CS_CANCEL_ALL);                                return retcode;                        }                        break;                     case CS_STATUS_RESULT:                        /*                        ** Not expecting CS_STATUS_RESULT in this example,                        ** but if received results will be pending. Therefore,                        ** cancel the current result set.                        */                        retcode = ct_cancel(NULL, cmd, CS_CANCEL_CURRENT);				RETURN_IF(retcode,"updatetextdata: ct_cancel");                        break;   		    case CS_CMD_SUCCEED:                    case CS_CMD_DONE:                        /*                        ** This means that the command succeeded or is                        ** finished.                        */                        break;                     case CS_CMD_FAIL:                        /*                        ** The server encountered an error while                        ** processing our command.                        */                        printf("UpdateTextData: ct_results: CS_CMD_FAIL") ;                        break;                    default:                        /* We got something unexpected.  */                        printf("UpdateTextData: ct_results: unexpected result");                        /* Cancel all results.  */                        ct_cancel(NULL, cmd, CS_CANCEL_ALL);                        break;                }        }         /*        ** We're done processing results. Let's check the        ** return value of ct_results() to see if everything        ** went ok.        */        switch ((int)retcode)        {                case CS_END_RESULTS:                        /*                        ** Everything went fine.                        */                        retcode = CS_SUCCEED;                        break;                 case CS_FAIL:                        /*                        ** ct_results() call failed.                        */                        printf("UpdateTextData: ct_results() failed");                default:                        /*                        ** We got an unexpected return value.                        */                        printf("UpdateTextData: ct_results: unexpected result");                        break;        }         return retcode;} /*** ProcessTimestamp()**** Purpose:**      This function retrieves the new timestamp for**      the updated text column into the CS_IODESC**      structure.**** Parameters:**      cmd             - Pointer to a CS_COMMAND structure.**      textdata        - Pointer to a TEXT_DATA structure to fill.** Returns:**      CS_SUCCEED if text was updated correctly.**      Otherwise a Client-Library failure code.*/ CS_RETCODE ProcessTimestamp(cmd, textdata)CS_COMMAND	*cmd;TEXT_DATA       *textdata;{        CS_RETCODE      retcode;        CS_INT          count, i = 1;        CS_DATAFMT      datafmt;         retcode = ct_describe(cmd, 1, &datafmt);		RETURN_IF(retcode, "processtimestamp - ct_describe ");         /*        ** Check if the data is a timestamp. If so, save it        ** to the CS_IODESC structure for future text updates.        */        if (!(datafmt.status & CS_TIMESTAMP))        {                /* Unexpected parameter data was received.  */                printf("ProcessTimestamp: unexpected parameter data received");                return CS_FAIL;        }         /*        ** Bind the timestamp field of the io descriptor        ** to assign the new timestamp from the parameter        ** results.        */        datafmt.maxlength = sizeof(textdata->iodesc.timestamp);        datafmt.format     = CS_FMT_UNUSED;        retcode = ct_bind(cmd, 1, &datafmt, 			(CS_VOID *)textdata->iodesc.timestamp,			&textdata->iodesc.timestamplen, NULL);		RETURN_IF(retcode, "processtimestamp - bind ");          /*        ** Retrieve the parameter result containing the timestamp.        */        retcode = ct_fetch(cmd, CS_UNUSED, CS_UNUSED, CS_UNUSED, &count);		RETURN_IF (retcode, "processtimestamp ct_fetch ");        if (retcode != CS_SUCCEED)        {                printf("ProcessTimestamp: ct_fetch() failed");                return retcode;        }         /* The timestamp was retrieved, so cancel the rest of the result set.        */        retcode = ct_cancel(NULL, cmd, CS_CANCEL_CURRENT);		RETURN_IF( retcode, "process timestamp - ct_cancel");        return CS_SUCCEED;}

⌨️ 快捷键说明

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