ORACLE数据导入C程序

#include <stdio.h>
#include <dbsql.h>

#define FLD_MAX_BUFF_LEN 2048
#define MAX_BUFF_NUM  1
#pragma pack(1)

char *ltrim( char * str)             /*去掉str右边的空格*/
{
    int len ;
    len = strlen (str);
    while (len > 0 && str[len -1 ] == ' ')
    {
        len --;
    }
    str[len] = '/0';
    return str;
}

typedef char *  FILED;

int main(int argc, char *argv[])
{
    HSTMT   hstmt,hstmt1;
    RETCODE rc;
    FILED   fields[2000];

    char tmp[3],tmpstr[64*1024 + 1],*tmp_pos;
    char tmpsql[9182];
    int  pcrow,i,j,k;
    int  num,max_buf_len,tot_num;
    double tm1,tm2,tm;
    FILE *fptr;
    SWORD pfSqlType,pibScale,pfNullable,counts;     /*#define SWORD short int*/
    UDWORD pcbColDef;
    int tmp_int;
    double tmp_double;
    int fieldlen[2000];
    int is_continue;
    int maxlen;

    UCHAR  szColName[100];
    SWORD  cbColNameMax=99;
    SWORD  pcbColName;

    if ( argc != 5 )
    {
        printf("Usage : load user passwd tablename tablefile /n");
        exit (-1);
    }
   
    fptr = fopen(argv[4],"rt");
    if (fptr == NULL)
    {
        printf(" open output file %s error/n" ,argv[4]);
        fflush(stdout);
        printf(" /t倒入表 %s 失败./n",argv[3]);
        return -1;
    }
   
    tm = get_milli_time();

    SQLAllocEnv(0 );
    rc = SQLConnect( 0,"",0,(UCHAR *)argv[1],0,(UCHAR *)argv[2],0);
    if (rc != SQL_SUCCESS)
    {
        printf(" /t倒入表 %s 失败./n",argv[3]);
        return -1;
    }

    rc = SQLAllocStmt(0,&hstmt);
    if (rc != SQL_SUCCESS)
    {
        printf("Alloc stmt is failed./n");
        printf(" /t倒入表 %s 失败./n",argv[3]);
        fflush(stdout);
        return -1;
    }

    rc = SQLAllocStmt(0,&hstmt1);
    if (rc != SQL_SUCCESS)
    {
        printf("Alloc stmt is failed./n");
        printf(" /t倒入表 %s 失败./n",argv[3]);
        fflush(stdout);
        return -1;
    }

    sprintf(tmpsql,"select * from %s where 1 != 1",argv[3]);
    rc = SQLPrepare(hstmt,(UCHAR *)tmpsql,0);
    if (rc != SQL_SUCCESS)
    {
        printf("sql is %s/n",tmpsql);
        printf(" /t倒入表 %s 失败./n",argv[3]);
        fflush(stdout);
        return -1;
    }

    rc = SQLExecute(hstmt);
    if (rc != SQL_SUCCESS)
    {
        printf("file is %s line is %d/n",__FILE__,__LINE__);
        printf(" /t倒入表 %s 失败./n",argv[3]);
        fflush(stdout);
        return -1;
    }

    SQLNumResultCols( hstmt,( SWORD  FAR *)&counts);
    num = 0;

   
sprintf(tmpsql,"insert into %s values ( :V0",argv[3]);
    for (i = 1; i < counts ; i++)
    {
        sprintf(tmpsql,"%s,:V%d",tmpsql,i);
    }
    strcat(tmpsql,")");

    for (i = 1; i <= counts ; i++)
    {
       
        SQLDescribeCol(hstmt,i,szColName,cbColNameMax,&pcbColName
                       ,&pfSqlType,&pcbColDef, &pibScale,&pfNullable);
        fieldlen[i - 1 ] = pibScale + 1;
        fields[i - 1] = (char *)malloc(MAX_BUFF_NUM * fieldlen[i-1] + 1);
        if (fields[i-1] == NULL)
        {
            printf(" /t内存不够,倒入表 %s 失败./n",argv[3]);
            fflush(stdout);
            return -1;
        }
    }

    rc = SQLPrepare(hstmt1,(UCHAR *)tmpsql,0);
    if (rc != SQL_SUCCESS)
    {
        printf("sql is %s/n",tmpsql);
        printf(" /t倒入表 %s 失败./n",argv[3]);
        fflush(stdout);
        return -1;
    }

    tot_num = 0;
    while (1)
    {
     
        tm1= get_milli_time();
        num = 0;
        for (i = 0; i < counts ; i++)
        {
            memset(fields[i],' ',MAX_BUFF_NUM * fieldlen[i]);
        }

        for ( k = 0 ; k < MAX_BUFF_NUM ; k++ )
        {
            memset(tmpstr,'/0',sizeof(tmpstr));
            is_continue = 1;
            if ( fgets(tmpstr,64*1024,fptr) == NULL)
            {
                is_continue = 0;
                break;
            }
            num++;
            tmp_pos = tmpstr;
            for (i = 0; i < counts ; i++)
            {
                j = 0;
                maxlen = 0;
                while ((*tmp_pos != '|') && maxlen < fieldlen[i]-1 )
                {
                    maxlen ++;
                    fflush(stderr);
                    fields[i][ k * fieldlen[i] + j] = *tmp_pos;
                    tmp_pos ++;
                    j++;
                }

                if ( j < fieldlen[i]-1 )
                {
                    j = fieldlen[i] - 1 ;
                    fields[i][ k * fieldlen[i] + j] = '/0';
                }
                  
                tmp_pos ++;
                /*
                if (fields[i][k * fieldlen[i]] == '/0')
                {
                    sprintf(&fields[i][k * fieldlen[i]] ,"NULL");
                }
                for (j = 0; j < fieldlen[i]  ; j++ )
                {
                    if ( fields[i][k * fieldlen[i] + j ] == '/0')
                    {
                        fields[i][k * fieldlen[i] + j ] = ' ';
                    }
                }
                */

            }
        }

        if ( num > 0 )
        {
            for ( i = 0 ; i < counts ; i++ )
            {
                if (SQLBindParameter(hstmt1,i+1,0,SQL_C_CHAR,0,0,0,
                     fields[i],fieldlen[i]-1 ,&max_buf_len) != SQL_SUCCESS)
                {
                    printf(" /t倒入表 %s 失败./n",argv[3]);
                    return -1;
                }
            }

            if (SQLExecuteEx(hstmt1,num) != SQL_SUCCESS)
            {
                printf(" /t倒入表 %s 失败,num is %d /n",argv[3],num);
                ROLLBACK_TRAN(SQL_NULL_HDBC);
                num = 0;
                break;
            }

            tot_num += num;
            COMMIT_TRAN(SQL_NULL_HDBC);
        }

        tm2= get_milli_time();
        if ( is_continue == 0)
        {
            break;
        }
    }   /*end while*/

    COMMIT_TRAN(SQL_NULL_HDBC);
    fclose(fptr);
    tm2= get_milli_time();

    printf("/t共有 %d 行被倒入,共耗时: %12.3lf 秒/n" , tot_num , tm2 - tm);
    SQLFreeStmt( hstmt, SQL_DROP);
    SQLFreeStmt( hstmt1, SQL_DROP);
    SQLFreeEnv(0);
    return 0;
}

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭