程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 一個簡單的oracle pro c/c++多線程導出程序

一個簡單的oracle pro c/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);
}

 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved