项目中经常需要用到此类型的工具
1 #pragma warning (disable:4786) 2 #include <iostream> 3 #include <map> 4 #include <string> 5 using namespace std; 6 #include <stdio.h> 7 #include <stdlib.h> 8 #include <algorithm> 9 #define OTL_ORA9I // Compile OTL 4.0/OCI9i 10 #define OTL_STREAM_READ_ITERATOR_ON 11 #define OTL_STL 12 13 #ifdef WIN32 14 #include <conio.h> 15 #else 16 //#define PASS_SECURITY 17 #endif 18 19 #ifdef PASS_SECURITY 20 #include <pass_security/TDBCrypt.h> //包含密码解析头文件 21 #else 22 #define DB_ENV_LEN 100 23 #endif 24 25 #include "otlv4.h" // include the OTL 4 header file 26 27 28 #define OTL_BUFFER_SIZE 2000 //otl缓冲行数 29 #define BUFFER_SIZE 2000 //列内容缓冲 30 31 32 33 int select(otl_connect& db, const string &sql, FILE *fp, const string &delemer = "|") //获得sql返回,并以|为分隔符输出到文件(第一行是列名) 34 { 35 36 // create select stream 37 otl_stream stream_i; 38 //将输出类型全部转成string 39 stream_i.set_all_column_types(otl_all_date2str | otl_all_num2str); 40 41 stream_i.open(OTL_BUFFER_SIZE, // buffer size 42 sql.c_str(), 43 // SELECT statement 44 db // connect object 45 ); 46 47 //文件头 48 int desc_len; 49 otl_column_desc *desc = stream_i.describe_select(desc_len); 50 for(int n = 0; n < desc_len; ++n){ 51 fprintf(fp, "%s%s", desc[n].name, delemer.c_str()); 52 } 53 fprintf(fp, " "); 54 55 //文件内容 56 string content; 57 int row_count = 0; 58 59 while (!stream_i.eof()) 60 { 61 for(int n = 0; n < desc_len; ++n){ 62 stream_i >> content; 63 fprintf(fp, "%s%s", content.c_str(), delemer.c_str()); 64 } 65 fprintf(fp, " "); 66 row_count++; 67 } 68 69 cerr << row_count << " rows loaded " << endl; 70 71 return 0; 72 } 73 74 75 int get_passwd(string& io_dbname) //通过用户名获得 user/pass@database 形式字符串 76 { 77 78 if (string::npos != io_dbname.find("/")) 79 { 80 return 0; 81 } 82 83 84 85 #ifdef PASS_SECURITY 86 std::string strDBConnect = io_dbname; 87 88 char sDB[DB_ENV_LEN + 1],sUSR[DB_ENV_LEN + 1],sPWD[DB_ENV_LEN + 1]; 89 security::TDBCrypt Crypt; 90 int iresult = Crypt.init(); 91 if (iresult) 92 { 93 cerr << "get passfile failed" << std::endl; 94 return 1; 95 } 96 97 if (Crypt.fetchUserPwd(io_dbname.c_str(), sUSR, sPWD, sDB)) 98 { 99 io_dbname = ""; 100 io_dbname = io_dbname + sUSR + "/" + sPWD + "@" + sDB; 101 return 0; 102 } 103 else 104 { 105 cerr << "get password failed" << std::endl; 106 return 1; 107 } 108 #else 109 110 #ifdef PASS_SECURITY 111 size_t nDBpos = io_dbname.find("@"); 112 113 string sUSR = io_dbname.substr(0, nDBpos); 114 string sDB = io_dbname.substr(nDBpos + 1, io_dbname.length() - 1); 115 char sPWD[DB_ENV_LEN + 1] = ""; 116 117 118 cerr << "please input password of "<< io_dbname << " : " << std::endl; 119 120 int ch; 121 size_t i = 0; 122 while (((ch = getch()) != ' ') && i < DB_ENV_LEN) 123 { 124 if (ch != '') 125 { 126 sPWD[i++] = (char) ch; 127 cerr << "*"; 128 } 129 else 130 { 131 if (i >= 1) 132 { 133 i--; 134 cerr << " "; 135 } 136 else 137 { 138 cerr << "