程序说明:

1,从dynamic_file.in中读入动态的sql语句,创建跟T_USER一样的表T_USER2,并且插入一条数据;
2,从dynamic_file1.in中读入动态的sql语句,删除T_USER2表;
3,把所有的sql语句内容,错误信息,时间存储在dynamic_file.log的日志文件中。

dynamic_file.pc

#include <iostream>
using namespace std;
#include "sqlca.h"
#include "fstream"
#include "time.h"

EXEC SQL BEGIN DECLARE SECTION;
char *uid="
soft/soft@192.168.0.2:1521/orasoft";
char sqlstmt[100];
//char buf[100]
EXEC SQL END DECLARE SECTION;

//fstream outfile("dynamic_file.log");

void sqlerr()
{
EXEC SQL WHENEVER SQLERROR CONTINUE;
fstream errlog("dynamic_file.log",ios::out|ios::app);
cout<<sqlca.sqlerrm.sqlerrmc<<endl;
time_t ltime;
time(&ltime);
//cout<<time<<endl;
errlog.write(ctime(&ltime),strlen(ctime(&ltime)));
errlog.write(sqlca.sqlerrm.sqlerrmc,strlen(sqlca.sqlerrm.sqlerrmc));
errlog<<‘\n';
EXEC SQL ROLLBACK WORK RELEASE;
errlog.close();
exit(1);
}

int main(int argc,char* argv[])
{
if(argc<2)
{
   cout<<"please input source file like:<filename> <source file>"<<endl;
   return 0;
}
EXEC SQL WHENEVER SQLERROR DO sqlerr();
EXEC SQL CONNECT :uid;

fstream infile(argv[1]);
cout<<"source file:"<<argv[1]<<endl;
if(infile==NULL)
{
   cout<<"the source file is empty.."<<endl;
   return 0;
}

fstream outfile("dynamic_file.log",ios::out | ios::app);
while(!infile.eof())
{
   //cout<<infile.get()<<endl;
   infile.getline(sqlstmt,100);
   cout<<"sqlstmt:"<<sqlstmt<<endl;
   EXEC SQL EXECUTE IMMEDIATE :sqlstmt;
   time_t ltime;
   time(&ltime);
   outfile<<ctime(&ltime);
   outfile.write(sqlstmt,strlen(sqlstmt));
   outfile<<‘\n';
   if(infile.peek()==-1)break;
}
EXEC SQL COMMIT WORK RELEASE;
infile.close();
outfile.close();
cout<<"————–OK————–"<<endl;
return 0;
}

dynamic_file.in

CREATE TABLE T_USER2 AS SELECT * FROM T_USER
INSERT INTO T_USER2 VALUES(100,’ABC’)

dynamic_file1.in

DROP TABLE T_USER2

有些地方还需要改进,比如把写log功能封装到一个类