用otl写的oracle取数工具,执行传入在查询语句,把结果输出到文件
项目中经常需要用到此类型的工具
#pragma warning (disable:4786)
#include <iostream>
#include <map>
#include <string>
using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#define OTL_ORA9I // Compile OTL 4.0/OCI9i
#define OTL_STREAM_READ_ITERATOR_ON
#define OTL_STL #ifdef WIN32
#include <conio.h>
#else
//#define PASS_SECURITY
#endif #ifdef PASS_SECURITY
#include <pass_security/TDBCrypt.h> //包含密码解析头文件
#else
#define DB_ENV_LEN 100
#endif #include "otlv4.h" // include the OTL 4 header file #define OTL_BUFFER_SIZE 2000 //otl缓冲行数
#define BUFFER_SIZE 2000 //列内容缓冲 int select(otl_connect& db, const string &sql, FILE *fp, const string &delemer = "|") //获得sql返回,并以|为分隔符输出到文件(第一行是列名)
{ // create select stream
otl_stream stream_i;
//将输出类型全部转成string
stream_i.set_all_column_types(otl_all_date2str | otl_all_num2str); stream_i.open(OTL_BUFFER_SIZE, // buffer size
sql.c_str(),
// SELECT statement
db // connect object
); //文件头
int desc_len;
otl_column_desc *desc = stream_i.describe_select(desc_len);
for(int n = ; n < desc_len; ++n){
fprintf(fp, "%s%s", desc[n].name, delemer.c_str());
}
fprintf(fp, "\n"); //文件内容
string content;
int row_count = ; while (!stream_i.eof())
{
for(int n = ; n < desc_len; ++n){
stream_i >> content;
fprintf(fp, "%s%s", content.c_str(), delemer.c_str());
}
fprintf(fp, "\n");
row_count++;
} cerr << row_count << " rows loaded " << endl; return ;
} int get_passwd(string& io_dbname) //通过用户名获得 user/pass@database 形式字符串
{ if (string::npos != io_dbname.find("/"))
{
return ;
} #ifdef PASS_SECURITY
std::string strDBConnect = io_dbname; char sDB[DB_ENV_LEN + ],sUSR[DB_ENV_LEN + ],sPWD[DB_ENV_LEN + ];
security::TDBCrypt Crypt;
int iresult = Crypt.init();
if (iresult)
{
cerr << "get passfile failed" << std::endl;
return ;
} if (Crypt.fetchUserPwd(io_dbname.c_str(), sUSR, sPWD, sDB))
{
io_dbname = "";
io_dbname = io_dbname + sUSR + "/" + sPWD + "@" + sDB;
return ;
}
else
{
cerr << "get password failed" << std::endl;
return ;
}
#else #ifdef PASS_SECURITY
size_t nDBpos = io_dbname.find("@"); string sUSR = io_dbname.substr(, nDBpos);
string sDB = io_dbname.substr(nDBpos + , io_dbname.length() - );
char sPWD[DB_ENV_LEN + ] = ""; cerr << "please input password of "<< io_dbname << " : " << std::endl; int ch;
size_t i = ;
while (((ch = getch()) != '\r') && i < DB_ENV_LEN)
{
if (ch != '\b')
{
sPWD[i++] = (char) ch;
cerr << "*";
}
else
{
if (i >= )
{
i--;
cerr << "\b \b";
}
else
{
cerr << "\007";
}
}
}
sPWD[i] = '\0';
cerr << endl; io_dbname = sUSR + "/" + sPWD + "@" + sDB;
#endif #endif return ;
} int main(const int argc, const char * const argv[])
{
if (argc < )
{
cerr << "参数错误!用法示例:\t" << endl;
cerr << "dtload user/pass@DB sql filename" << endl;
return ;
} map<string, string> mpParam;
for (int i = ; i < argc; i++)
{
if (*argv[i] == '-')
{
if (i < argc)
{
mpParam[argv[i]] = argv[i + ];
}
else
{
mpParam[argv[i]] = "";
}
}
} otl_connect db; string dbname = argv[];
string sql = argv[];
string file = "";
FILE *fp = NULL; //如果输出文件名为空,则输出到屏幕
if (argc >= )
{ file = argv[];
fp = fopen(file.c_str(), "w"); if (fp == NULL)
{
cerr << "openfile " << file << " err! maybe can not writed\n ";
return ;
}
}
else
{
fp = stdout;
} string delemer = "|";
if (argc >= )
{
delemer = argv[];
} //如果输入参数不包含密码,则从dbfile里面找密码
//get_passwd(dbname); otl_connect::otl_initialize(); // initialize OCI environment try
{
db.rlogon(dbname.c_str()); // connect to Oracle
select(db, sql, fp, delemer); // select records from table
}
catch(const otl_exception& p)
{ // intercept OTL exceptions
cerr<<p.msg<<endl; // print out error message
cerr<<p.stm_text<<endl; // print out SQL that caused the error
cerr<<p.var_info<<endl; // print out the variable that caused the error
} fclose(fp);
db.logoff(); // disconnect from Oracle }
Makefile
## -*- Makefile -*-
## by hch top_srcdir = ../ DISPATCH = $(bindir)/Dispatch TARGETS = tbasload include $(top_srcdir)/MKFILE/Make.rules OBJS = tbasload.o CPPFLAGS := \
$(ORACLE_INCLUDE) \
-I. \
$(CPPFLAGS) LDFLAGS := \
$(LDFLAGS) \
$(ORACLE_LDFLAGS) tbasload: $(OBJS)
@echo "Creating executable -*- $@ -*- ..."
@-rm -f $@
@$(CXX) -o $@ $(LDFLAGS) $(ORACLE_LIBS) $^
Make.rules
# -*-Make.rules-*-
# by szh buildbits ?=
debug ?= STATICLIBS = yes VERSION = 1.0.
SOVERSION =
ifeq ($(buildbits),)
bindir = $(top_srcdir)/bin
libdir = $(top_srcdir)/lib
else
bindir = $(top_srcdir)/bin32
libdir = $(top_srcdir)/lib32
endif
includedir = $(top_srcdir)/include/common UNAME := $(shell uname) include $(top_srcdir)/MKFILE/Make.rules.$(UNAME)
include $(top_srcdir)/MKFILE/Make.rules.Customized CPPFLAGS = -I$(includedir) $(ORACLE_INCLUDE)
LDFLAGS := $(LDFLAGS) $(CXXFLAGS) -L$(libdir) ifeq ($(mklibfilename),)
mklibfilename = $(if $(),lib$().so.$(),lib$().so)
endif ifeq ($(mksoname),)
mksoname = $(if $(),lib$().so.$(),lib$().so)
endif ifeq ($(mklibname),)
ifeq ($(STATICLIBS),yes)
mklibname = lib$().a
else
mklibname = lib$().so
endif
endif ifndef mklibtargets
ifeq ($(STATICLIBS),yes)
mklibtargets = $()
else
mklibtargets = $() $() $()
endif
endif ifeq ($(mkshlib),)
$(error You need to define mkshlib in Make.rules.$(UNAME))
endif EVERYTHING = all clean .SUFFIXES: %.o: %.cpp
@echo " $<"
@$(CXX) -o $@ -c $(CPPFLAGS) $(CXXFLAGS) $< %.o: %.c
@echo " $<"
@$(CC) -o $@ -c $(CPPFLAGS) $(CFLAGS) $< all:: warning $(SRCS) $(TARGETS) warning::
@echo
@echo "CONFIGURATION: buildbits=$(buildbits) debug=$(debug) STATICLIBS=$(STATICLIBS)"
@echo clean::
@-rm -f $(TARGETS) $(OBJS)
@-rm -f core *.o version.h
用otl写的oracle取数工具,执行传入在查询语句,把结果输出到文件的更多相关文章
- Oracle 取上周一到周末日期的查询语句
-- Oracle 取上周一到周末的sql -- 这样取的是 在一周内第几天,是以周日为开始的 select to_char(to_date('20130906','yyyymmdd'),'d') f ...
- 如何在Oracle中一次执行多条sql语句 (.net C#)
关键是不能换行,要加上begin ...sql... end; 每个SQL用:隔开,end后面必须加: 以下是拷贝于:http://www.cnblogs.com/teamleader/arc ...
- oracle 实现多字段匹配一个关键字查询语句
oracle 实现多字段匹配一个关键字查询语句:有两种方法(经测试,10g中不能用,11g才行): 第一种. select * from table where ('字段名1' ||'字段名2' || ...
- Oracle学习(一):基本操作和基本查询语句
文中以"--"开头的语句为凝视,即为绿色部分 1.知识点:能够对比以下的录屏进行阅读 SQL> --录屏工具spool,開始录制,并指定保存路径为c:\基本查询.txt SQ ...
- ORACLE中关于表的一些特殊查询语句
1: 如何判断字段的值里面:那些数据包含小写字母或大小字母 判断字段NAME的值里面有小写字母的记录 方式1: SELECT NAME FROM TEST WHERE regexp_like(NAME ...
- (学)如何在Oracle中一次执行多条sql语句
队长同学原来的地址:https://www.cnblogs.com/teamleader/archive/2007/05/31/765943.html队长同学原来的描述: 有时我们需要一次性执行多条s ...
- Mysql中类似于Oracle中connect by ... start with的查询语句(木大看懂)
表结构 create table sys_branch ( id ) not null, parent_id ), branch_name ), delete_flag ), primary key ...
- 40多个非常有用的Oracle 查询语句
给大家介绍是40多个非常有用的Oracle 查询语句,主要涵盖了日期操作,获取服务器信息,获取执行状态,计算数据库大小等等方面的查询.这些是所有Oracle 开发者都必备的技能,所以快快收藏吧! 日期 ...
- 45 个非常有用的 Oracle 查询语句
这里我们介绍的是 40+ 个非常有用的 Oracle 查询语句,主要涵盖了日期操作,获取服务器信息,获取执行状态,计算数据库大小等等方面的查询.这些是所有 Oracle 开发者都必备的技能,所以快 ...
随机推荐
- POJ 2007 Scrambled Polygon 凸包
Scrambled Polygon Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 7214 Accepted: 3445 ...
- 洛谷P1993 小 K 的农场(查分约束)
/* 加深一下对查分约束的理解 建图的时候为了保证所有点联通 虚拟一个点 它与所有点相连 权值为0 然后跑SPFA判负环 这题好像要写dfs的SPFA 要不超时 比较懒 改了改重复进队的条件~ */ ...
- jQuery中在当前页面弹出一个新的界面
W.$.dialog({ content:'url:wswgrkbillController.do?snh&id='+b+'&bh='+c+'&ck='+d+'&sl= ...
- SQL SERVER 2012疑难问题解决方法
问题一: 问题描述 SQL SERVER 2012 尝试读取或写入受保护的内存.这通常指示其他内存已损坏. (System.Data) 解决办法 管理员身份运行 cmd -> 输入 netsh ...
- Data Types
原地址: Home / Database / Oracle Database Online Documentation 11g Release 2 (11.2) / Database Administ ...
- [转] iOS TableViewCell 动态调整高度
原文: http://blog.csdn.net/crayondeng/article/details/8899577 最近遇到了一个cell高度变化的问题,在找解决办法的时候,参考了这篇文章,觉得不 ...
- python下 help()使用方法
查看python所有的modules:help("modules") 单看python所有的modules中包含指定字符串的modules: help("modules ...
- hdu 1548 A strange lift (bfs)
A strange lift Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) T ...
- 13 Roman to Integer(罗马数字转int Easy)
题目意思:罗马数字转int 思路:字符串从最后一位开始读,IV:+5-1 class Solution { public: int romanToInt(string s) { map<char ...
- javascript 16/1/14随记
1.想在一个事件或者函数之后,触发某个事件. var flag=false //定义一个全局变量 function aku(){ if(flag){ } } sizemousedown=functio ...