oracle OCI fetch data have blank characters

140 views Asked by At

OCI fetch data from oracle database , the data field have a problem(actually it's not a problem) , for example: if my table defined like this:

create table t_test (
text_field varchar2(35),
....
);

if i use a buffer to retrive the data field fetched from oracle database , the oci would automaticlly add a few blank characters , here some fetch code:

void SingleFetch_Test()
{
    ROCIDataSet oci_dataset={0};
    char query_clause[1024]= "select distributorcode,senddirectory from t_exppath order by distributorcode";
    char exp_code[15]="";
    char exp_path[256]="";
    sword retcode=0;
    
    SetDataSetClause(&oci_dataset,query_clause);//OCIStmtPrepare
    //Prepare->OCIDefineByPos->OCIStmtExecute...
    ub2 dbtype;
    
    retcode=OpenOCIDataSet(&oci_dataset);//OCIStmtExecute
    {
        OCIParam *param;
        ub4 index=1;
        ub2 type,type_length;
        text* column_name;
        ub4 col_name_len;
        int col_count=0;
        
        OCIAttrGet(oci_dataset.stmt,OCI_HTYPE_STMT,(void*)&col_count,(ub4*)0,(ub4)OCI_ATTR_PARAM_COUNT,oci_dataset.err);
        printf("count:%d columns\n",col_count);       
        while(OCI_SUCCESS==OCIParamGet(oci_dataset.stmt,OCI_HTYPE_STMT,oci_dataset.err,(void**)&param,index)) {
            OCIAttrGet((void*)param,
                       (ub4)OCI_DTYPE_PARAM,
                       (void**)&column_name,
                       (ub4*)&col_name_len,
                       (ub4)OCI_ATTR_NAME,
                       oci_dataset.err);
                       
            OCIAttrGet((void*)param,
                       (ub4)OCI_DTYPE_PARAM,
                       (void*)&type,
                       (ub4*)0,
                       (ub4)OCI_ATTR_DATA_TYPE,
                       oci_dataset.err);
                       
            OCIAttrGet((void*)param,
                       (ub4)OCI_DTYPE_PARAM,
                       (void*)&type_length,
                       (ub4*)0,
                       (ub4)OCI_ATTR_DATA_SIZE,
                       oci_dataset.err);
            
            //OCI_ATTR_PRECISION
            //OCI_ATTR_SCALE
            //OCI_ATTR_IS_NULL
            printf("%s:(%s,%d)\n",column_name,(type==OCI_TYPECODE_VARCHAR2?"characters":"other"),type_length);
            index++;
        }
    }
    OCIDefine* pdef=(OCIDefine*)0;
    dbtype=SQLT_CHR;//SQLT_STR;
    OCIDefineByPos(oci_dataset.stmt,&pdef,oci_dataset.err,1,(dvoid*)exp_code,(sword)sizeof(exp_code),dbtype,(dvoid*)0,(ub2*)0,(ub2*)0,OCI_DEFAULT);
    dbtype=SQLT_CHR;//SQLT_STR;
    OCIDefineByPos(oci_dataset.stmt,&pdef,oci_dataset.err,2,(dvoid*)exp_path,(sword)sizeof(exp_path),dbtype,(dvoid*)0,(ub2*)0,(ub2*)0,OCI_DEFAULT);
    
    while(TRUE) {
        if(retcode==OCI_NO_DATA) {
            break;
        }
        else if(retcode==OCI_SUCCESS||retcode==OCI_SUCCESS_WITH_INFO) {
            printf("[%s],[%s]\n",exp_code,exp_path);
            
            memset(exp_code,0x00,sizeof(exp_code));
            memset(exp_path,0x00,sizeof(exp_path));
            retcode=OCIStmtFetch(oci_dataset.stmt,oci_dataset.err,1,OCI_FETCH_NEXT, OCI_DEFAULT);
        }
        else {
            GetOCIDataSetError(&oci_dataset);
            printf("%s\n",oci_dataset.error_desc);
            break;
        }        
    }
    
    CloseOCIDataSet(&oci_dataset);    
}

and the table defination:

create table T_EXPPATH
(
  distributorcode VARCHAR2(3) not null,
  distributorname VARCHAR2(80),
  senddirectory   VARCHAR2(120)
)

and the output:

[999            ],[F:\data\TestFolder\                                                                        ]       

what i hope: [999],[F:\data\TestFolder]

if there is a numberic data field , i could use oci api to convert it into a text buffer , and there are also some blank characters filled the right side. if i use some memory buffer to cache the fetch result. the blank characters have nothing good but consume the memory size. is there a way to deal with this blank charactors?

0

There are 0 answers