📄 mult_text.c
字号:
/* Program Name : mult_text.c **** Description : This program demonstrates the use of ct-library text/image ** handling calls to update multiple columns and rows of data.** It inserts data into an existing table and updates both text ** columns. It then displays the text data from the table.**** References : Open Client Client-Library/C Reference manual. Refer to** notes on Text and Image, ct_get_data, ct_data_info, ct_command** and the CS_IODESC structure.** ** Script file : mult_text.sql ***/#include <stdio.h>#include <ctpublic.h>#include <cspublic.h>#include <ospublic.h>#include "./example.h"/* Macro definitions for error handling */#define RETURN_IF(a,b) if (a != CS_SUCCEED) {fprintf(stdout,"error in %s\n",b); return a;}#define EXIT_IF(a) if (a != CS_SUCCEED) {fprintf(stdout,"FATAL error on line %d \n",__LINE__); exit(-1) ;}#define MAX_COLUMN 10 #define MAX_COLSIZE 255#define MAX_COMMAND 255#define EX_TXT_UPD1_VALUE "Row updated!"#define EX_TXT_UPD2_VALUE "Second column updated too!"/* Global definitions */typedef struct _text_data{ CS_IODESC iodesc; /* iodesc associated with text value */ CS_TEXT textbuf[255]; /* holds the value */ CS_INT textlen; /* number of bytes in textbuf */} TEXT_DATA;char rowbuffer[MAX_COLUMN][MAX_COLSIZE] ;CS_CONNECTION *conn_ptr2; /* 2nd connection to perform the update *//* Function delcarations */CS_RETCODE init_db PROTOTYPE(( CS_CONTEXT ** )); CS_RETCODE connect_db PROTOTYPE(( CS_CONTEXT *, CS_CONNECTION **, CS_CHAR *, CS_CHAR * ));CS_RETCODE cleanup_db PROTOTYPE(( CS_CONTEXT *, CS_RETCODE ));CS_RETCODE send_sql PROTOTYPE(( CS_COMMAND *, CS_CHAR * ));CS_RETCODE handle_returns PROTOTYPE(( CS_COMMAND * ));CS_RETCODE bind_columns PROTOTYPE(( CS_COMMAND * ));CS_RETCODE fetch_n_print PROTOTYPE(( CS_COMMAND *, TEXT_DATA *, TEXT_DATA * ));CS_RETCODE ProcessTimestamp PROTOTYPE(( CS_COMMAND *, TEXT_DATA * ));CS_RETCODE updatetextdata PROTOTYPE (( TEXT_DATA *, char * ));CS_VOID DisplayData PROTOTYPE (( TEXT_DATA * ));extern CS_RETCODE CS_PUBLIC ctlib_client_msg_handler PROTOTYPE(( CS_CONTEXT *, CS_CONNECTION *, CS_CLIENTMSG * ));extern CS_RETCODE CS_PUBLIC ctlib_server_msg_handler PROTOTYPE(( CS_CONTEXT *, CS_CONNECTION *, CS_SERVERMSG * ));int main(){ CS_CONTEXT *cntx_ptr ; CS_CONNECTION *conn_ptr ; CS_CHAR commandbuff[MAX_COMMAND] ; CS_COMMAND *cmd_ptr ; CS_RETCODE ret ; CS_INT command_type ; /* Initialize context */ ret = init_db(&cntx_ptr); EXIT_IF(ret); /* Open 2 connections to the database */ ret = connect_db (cntx_ptr, &conn_ptr, EX_USERNAME, EX_PASSWORD); EXIT_IF(ret); ret = connect_db (cntx_ptr, &conn_ptr2, EX_USERNAME, EX_PASSWORD); EXIT_IF(ret); /* Allocate a command structure */ ret = ct_cmd_alloc (conn_ptr, &cmd_ptr ); EXIT_IF(ret); /* Send the command to insert a row into the test table */ strcpy( commandbuff , "insert tempdb..test values(1,'hello world','hello worl')"); ret = send_sql (cmd_ptr, commandbuff); EXIT_IF(ret); /* Process results from the server */ ret = handle_returns ( cmd_ptr ); EXIT_IF(ret); /* Send the command to display the rows we just updated */ strcpy(commandbuff , "select * from tempdb..test"); ret = send_sql (cmd_ptr, commandbuff); EXIT_IF(ret); /* Process results from the server */ ret = handle_returns ( cmd_ptr ); EXIT_IF(ret); /* Drop the command structure */ ret = ct_cmd_drop ( cmd_ptr ); EXIT_IF(ret); /* Close both connections */ ret = ct_close ( conn_ptr, CS_UNUSED ) ; EXIT_IF(ret); ret = ct_close ( conn_ptr2, CS_UNUSED ) ; EXIT_IF(ret); /* Drop the context and do general cleanup */ ret = cleanup_db (cntx_ptr, ret ); EXIT_IF ( ret ); printf("\n End of program run! \n"); exit(0);}CS_RETCODE init_db (cntx_ptr)CS_CONTEXT **cntx_ptr ;{ CS_RETCODE retcode ; /* allocate a context */ retcode = cs_ctx_alloc (CS_VERSION_100,cntx_ptr ); RETURN_IF ( retcode, "cs_ctx_alloc "); /* initialize the library */ retcode = ct_init (*cntx_ptr, CS_VERSION_100 ); RETURN_IF ( retcode, "ct_init "); /* install the server message callback */ retcode = ct_callback ( *cntx_ptr, NULL, CS_SET, CS_SERVERMSG_CB, (CS_VOID *)ctlib_server_msg_handler) ; RETURN_IF ( retcode, "ct_callback "); /* install the client message callback */ retcode = ct_callback (*cntx_ptr, NULL, CS_SET, CS_CLIENTMSG_CB, (CS_VOID *)ctlib_client_msg_handler); RETURN_IF ( retcode, "ct_callback: CLIENTMSG "); return retcode ;}CS_RETCODE cleanup_db ( cntx_ptr, status )CS_CONTEXT *cntx_ptr ;CS_RETCODE status ;{ CS_RETCODE retcode ; CS_INT exit_type ; exit_type = ( status != CS_SUCCEED ) ? CS_FORCE_EXIT : CS_UNUSED ; /* close and cleanup connection to the server */ retcode = ct_exit (cntx_ptr, exit_type ); RETURN_IF ( retcode, "db_cleanup:ct_exit "); /* drop the context */ retcode = cs_ctx_drop (cntx_ptr) ; RETURN_IF ( retcode, "db_cleanup:cs_drop "); return retcode ; } CS_RETCODE connect_db (cntx_ptr, conn_ptr, user_name, password )CS_CONTEXT *cntx_ptr ;CS_CONNECTION **conn_ptr ;CS_CHAR *user_name, *password ;{ CS_RETCODE retcode ; /* Allocate a connection pointer */ retcode = ct_con_alloc (cntx_ptr, conn_ptr ); RETURN_IF ( retcode, "connect_db: ct_con_alloc "); /* Set the username and password properties */ retcode = ct_con_props ( *conn_ptr, CS_SET, CS_USERNAME, user_name, CS_NULLTERM, NULL ); RETURN_IF ( retcode, "connect_db: ct_con_props: username "); retcode = ct_con_props ( *conn_ptr, CS_SET, CS_PASSWORD, password, CS_NULLTERM, NULL ); RETURN_IF ( retcode, "connect_db: ct_con_props: password"); /* connect to the server */ retcode = ct_connect ( *conn_ptr, NULL, 0 ) ; RETURN_IF ( retcode, "connect_db: ct_connect ") ; return CS_SUCCEED ;}CS_RETCODE send_sql (cmd_ptr, commandbuff)CS_COMMAND *cmd_ptr ;CS_CHAR *commandbuff;{ CS_RETCODE retcode ; CS_DATAFMT datafmt ; printf("Command = %s\n",commandbuff); /* Build and send the command to the server */ retcode = ct_command ( cmd_ptr, CS_LANG_CMD, commandbuff, strlen(commandbuff), CS_UNUSED ); RETURN_IF ( retcode, "ct_command "); retcode = ct_send ( cmd_ptr ); RETURN_IF ( retcode, "ct_send "); return CS_SUCCEED ;}CS_RETCODE handle_returns ( cmd_ptr )CS_COMMAND *cmd_ptr ;{ CS_RETCODE results_ok ; CS_INT result_type ; TEXT_DATA textdata, textdata2; /* Process all returned result types */ while (( results_ok = ct_results ( cmd_ptr, &result_type)) == CS_SUCCEED ) { switch ((int)result_type) { case CS_ROW_RESULT: printf("TYPE: ROW RESULT \n"); results_ok = fetch_n_print ( cmd_ptr , &textdata, &textdata2); RETURN_IF ( results_ok, "fetch_n_print"); break ; case CS_CMD_SUCCEED: printf("TYPE: CMD SUCCEEDED \n"); break ; case CS_CMD_DONE : printf("TYPE : CMD DONE \n"); break ; case CS_CMD_FAIL : printf("TYPE: CMD FAIL \n"); break ; default : RETURN_IF ( CS_FAIL, "unknown results \n"); break ; } ; } if ( results_ok == CS_END_RESULTS ) return CS_SUCCEED ; return CS_FAIL ;}CS_RETCODE fetch_n_print (cmd_ptr , textdata, textdata2)CS_COMMAND *cmd_ptr ;TEXT_DATA *textdata, *textdata2;{ CS_RETCODE retcode ; CS_INT rows, i , num_cols ,len, len2; CS_DATAFMT target_format ; CS_TEXT *txtptr, *txtptr2; retcode = ct_res_info ( cmd_ptr, CS_NUMDATA, &num_cols, CS_UNUSED, NULL) ; RETURN_IF ( retcode, "ct_bind : ct_res_info "); memset ( &target_format, 0, CS_SIZEOF(target_format)) ; target_format.datatype = CS_CHAR_TYPE ; target_format.maxlength = MAX_COLSIZE ; target_format.count = 1 ; target_format.format = CS_FMT_NULLTERM ; /* Bind returned data to host variables */ retcode = ct_bind ( cmd_ptr, 1, &target_format, rowbuffer[0], NULL, NULL ) ; RETURN_IF ( retcode, "bind_columns: ct_bind "); /* Fetch the bound data into host variables */ while (( (retcode = ct_fetch(cmd_ptr, CS_UNUSED, CS_UNUSED, CS_UNUSED, &rows)) == CS_SUCCEED ) || ( retcode == CS_ROW_FAIL )) { if ( retcode == CS_ROW_FAIL )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -