unix 下多线程导出数据的pro c++程序

33 篇文章 1 订阅
26 篇文章 0 订阅
一个简单的oracle pro c/c++多线程导出程序

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <fcntl.h>
#include <malloc.h>
#include <dirent.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <fnmatch.h>
#include<pthread.h>
#include<iostream.h>

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"<<endl;
       
        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:"<<sp->fileName<<endl;
        }
       
        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"<<endl;
        }
       
        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<this_count;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<this_count;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"<<endl;
       
       
        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);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值