#include "OTLHelper.h"
OTLHelper::OTLHelper()
{
//初始化ODBC环境
otl_connect::otl_initialize();
}
OTLHelper::OTLHelper(const string strConn)
{
this->conn_str=strConn;
otl_connect::otl_initialize();
}
OTLHelper::~OTLHelper(){}
void OTLHelper::connect()
{
try
{
db.rlogon(this->conn_str.c_str());
}
catch(otl_exception& p)
{
throw p;
}
}
void OTLHelper::connect(const string user,const string passwd,const string dsn)
{
this->conn_str=user+"/"+passwd+"@"+dsn;
connect();
}
void OTLHelper::connect(const string strConn)
{
this->conn_str=strConn;
connect();
}
void OTLHelper::disconnect()
{
// disconnect from the database
db.logoff();
}
//表中的一条记录的数据结构
struct entry_item
{
string timestamp;
int millisecond;
double value;
int quality_code;
};
typedef struct entry_item Entry;
//导入CVS文件到对应表
//参数:文件路径 表名
void OTLHelper::import(const string csv_filepath,const string table)
{
fstream fs;
string line;
string temp;
stringstream ss_convert;
Entry item;
vector<Entry> test_data;
fs.open(csv_filepath.c_str(),ios_base::in);
//解析csv文件,读入一行,再使用stringstream截取","
try
{
int n=0;
while(getline(fs,line))
{
stringstream lineStream(line);
while(getline(lineStream,temp,','))
{
ss_convert<<temp;
//转换为对应的数据类型
switch(n++)
{
case 0:
ss_convert>>item.timestamp;
break;
case 1:
ss_convert>>item.millisecond;
break;
case 2:
ss_convert>>item.value;
break;
case 3:
ss_convert>>item.quality_code;
break;
}
ss_convert.clear();
ss_convert.str("");
}
test_data.push_back(item);
n=0;
}
}
catch(exception e)
{
cout<<e.what()<<endl;
}
temp.empty();
temp="insert into "+table+" values(:timestamp<char[9]>,:millisecond<int>,:value<double>,:quality_code<int>)";
otl_stream o(1,
temp.c_str(),
db);
int count=test_data.size();
//循环插入数据到MYSQL
for(int i=0;i<count;i=i+4)
{
try
{
o<<test_data[i].timestamp.c_str()
<<test_data[i].millisecond
<<test_data[i].value
<<test_data[i].quality_code;
}
catch(otl_exception& p)
{
throw p;
}
}
}
//查询,并输出结果
void OTLHelper::select(const string sql)
{
char timestamp[9];
int millisecond;
double value;
int quality_code;
try
{
otl_stream i(50,sql.c_str(),db); // buffer size may be > 1,SELECT statement,connect object
// while not end-of-data
while(!i.eof())
{
i>>timestamp;
cout<<"timestamp="<<timestamp;
i>>millisecond;
cout<<", millisecond="<<millisecond;
i>>value;
cout<<", value="<<value;
i>>quality_code;
cout<<", quality_code="<<quality_code;
cout<<"\n";
}
}
catch(otl_exception &p)
{
throw p;
}
}
void OTLHelper::import(const string sql_filepath)
{
//循环执行OTLHelper::insert(sql);
}
//导出数据
void OTLHelper::export(const string filepath,const string table)
{
}
void OTLHelper::update(const string sql)
{
try
{
otl_stream o(1,sql.c_str(),db);// buffer size,UPDATE statement,connect object
}
catch(otl_exception &p)
{
throw p;
}
}
void OTLHelper::insert(const string sql)
{
}
//直接执行sql
void OTLHelper::direct_exec(const string sql)
{
try
{
otl_cursor::direct_exec(db,sql.c_str());
}
catch(otl_exception& p)
{
throw p;
}
}
//直接执行sql,有参数版本,例如otl_exception::disabled
void OTLHelper::direct_exec(const string sql,const int arg)
{
try
{
otl_cursor::direct_exec(db,sql.c_str(),arg);
}
catch(otl_exception& p)
{
throw p;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页