Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19002129
  • 博文数量: 7460
  • 博客积分: 10434
  • 博客等级: 上将
  • 技术积分: 78178
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-02 22:54
文章分类

全部博文(7460)

文章存档

2011年(1)

2009年(669)

2008年(6790)

分类: 数据库开发技术

2008-05-28 08:38:28

一个简单的oracle pro c/c++多线程导出程序

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

EXEC SQL INCLUDE sqlca;

EXEC SQL BEGIN DECLARE SECTION;
struct paramater
{
        sql_context stx;
        char table[16+1];
        char fileName[16+1];
};
EXEC SQL END DECLARE SECTION;

int connectDB(char * pUserName, char * pPassWord, char * pDBName,sql_context ctx)
{
EXEC SQL BEGIN DECLARE SECTION;
        char tUserName[32+1];
        char tPassWord[32+1];
        char tDBName[32+1];
        char tDBCon[32+1];
EXEC SQL END DECLARE SECTION;

memset(tUserName,0,sizeof(tUserName));
strcpy(tUserName,pUserName);
memset(tPassWord,0,sizeof(tPassWord));
strcpy(tPassWord,pPassWord);
memset(tDBName,0,sizeof(tDBName));
strcpy(tDBName,pDBName);
 EXEC SQL CONTEXT USE :ctx; 
EXEC SQL WHENEVER SQLERROR continue;
EXEC SQL CONNECT :tUserName IDENTIFIED BY :tPassWord USING :tDBName;
if ( sqlca.sqlcode !=0 ){

        return -1;
        }

return 0;
}

int closeDB(sql_context ctx)
{
 EXEC SQL CONTEXT USE :ctx; 
EXEC SQL COMMIT WORK RELEASE;
return 0;
}

extern "C"
void * exportTable(void * p)
{
        cout<<"thread begin"<       
        FILE *fp;
        struct sqlca sqlca;
        char table[16+1];
       
        EXEC SQL BEGIN DECLARE SECTION;
        struct expname
        {
                char name[16+1];
        };
        struct expname *str;
        char strSql[1024+1];
        int this_count;
        struct paramater *sp;
        sql_context ctx;
        EXEC SQL END DECLARE SECTION;
       
        this_count=65535;
        int row_begin=0;
       
        sp=(struct paramater *)p;
        ctx=sp->stx;
       
        fp=fopen(sp->fileName,"w");
        if(fp == NULL)
        {
                cout<<"create file error:"<fileName<        }
       
        memset(strSql,0,sizeof(strSql));
        memset(table,0,sizeof(table));
        strncpy(table,sp->table,sizeof(table));
        sprintf(strSql,"select svcnum from info.T_CI_USERINFO_B");
       
        str=(struct expname *)calloc(65535,sizeof(struct expname));
        if(str == NULL)
        {
                cout<<"calloc str error"<        }
       
        EXEC SQL CONTEXT USE :ctx;
       
        EXEC SQL PREPARE ccsql FROM :strSql;
        EXEC SQL DECLARE curSql CURSOR FOR ccsql;
        EXEC ORACLE OPTION (PREFETCH=65535);
        EXEC SQL OPEN curSql;
       
        for(;;)
        {
                EXEC SQL FOR :this_count FETCH curSql INTO :str;
                this_count=sqlca.sqlerrd[2]-row_begin;
                row_begin=sqlca.sqlerrd[2];
               
                if(this_count == 0)
                {
                        break;
                }
               
                if(row_begin%this_count ==0 && sqlca.sqlcode == 0)
                {
                        for(int i=0;i                        {
                                fprintf(fp,"%s\n",(str+i)->name);
                        }
                }
               
                if(sqlca.sqlcode !=0)
                {
                        EXEC SQL for :this_count FETCH curSql INTO :str;
                       
                               
                        for(int i=0;i                        {
                                fprintf(fp,"%s\n",(str+i)->name);
                        }
                }
               
        }
       
       
        fprintf(fp,"%d\n",row_begin);
        fclose(fp);
        free(str);
       
        EXEC SQL CLOSE curSql;
        EXEC ORACLE OPTION (PREFETCH=1);
       
        cout<<"thread end"<       
       
        return (NULL);
}


int main()
{
        EXEC SQL BEGIN DECLARE SECTION;
        sql_context ctx[2];
        struct paramater pa[2];
        EXEC SQL END DECLARE SECTION;
       
        pthread_t pt[2];
       
        for(int i=0;i<2;i++)
        {
                EXEC SQL ENABLE THREADS;
                EXEC SQL CONTEXT ALLOCATE :ctx[i];
                connectDB((char *)"bill",(char *)"bill",(char *)"hntestbl",ctx[i]);
               
                sprintf(pa[i].table,"%s","T_CI_USERINFO_B");
                sprintf(pa[i].fileName,"%s%d","ttttt",i+1);
                pa[i].stx=ctx[i];
               
                pthread_create(&pt[i],NULL,exportTable,(void *)&pa[i]);

        }
       
        for(int j=0;j<2;j++)
        {

                pthread_join(pt[j],NULL);
                closeDB(ctx[j]);
                EXEC SQL CONTEXT FREE :ctx[j];
        }
        exit(0);
}
阅读(431) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~