封装MySQL++类,实现一些数据库基本操作,还包括大二进制文件的读写。
- #include "StdAfx.h"
- #include "DataBase.h"
- #include <string>
- #include <iostream>
- #include <fstream>
- using namespace std;
- using namespace mysqlpp;
- CDataBase::CDataBase( )
- {
- }
- void CDataBase::SetConnectionString(string dataBaseName,string serverIP, string userName,string userPassword)
- {
- m_DBname=dataBaseName;
- m_ServerIP=serverIP;
- m_UserName=userName;
- m_Password=userPassword;
- }
- bool CDataBase::Open()
- {
- try
- {
- m_ConnObject.connect(m_DBname.c_str(),m_ServerIP.c_str(),m_UserName.c_str(),m_Password.c_str());
- return true;
- }
- catch (BadOption &er)
- {
- return false;
- }
- catch (ConnectionFailed &er)
- {
- return false;
- }
- catch (Exception &er)
- {
- return false;
- }
- }
- void CDataBase::BeginTransaction()
- {
- try
- {
- if (m_ConnObject.connected())
- {
- m_pTrans = new Transaction(m_ConnObject);//nodelete
- }
- }
- catch (Exception &er)
- {
- }
- }
- void CDataBase::Commit()
- {
- try
- {
- if (m_ConnObject.connected())
- {
- m_pTrans->commit();
- delete m_pTrans;
- }
- }
- catch (Exception &er)
- {
- }
- }
- void CDataBase::Rollback()
- {
- try
- {
- if (m_ConnObject.connected())
- {
- m_pTrans->rollback();
- delete m_pTrans;
- }
- }
- catch (Exception &er)
- {
- }
- }
- int CDataBase::MaxID(string table,string idname)
- {
- int maxid=0;
- try
- {
- Query query=m_ConnObject.query();
- query<<"select max("<<idname<<") from "<<table;
- //query<<"select max(pId) from part";
- if(UseQueryResult res=query.use())
- {
- Row row=res.fetch_row();
- if (row[0])
- {
- maxid=row[0];
- }
- else
- {
- maxid=0;
- }
- }
- else
- {
- maxid=0;
- }
- return maxid;
- }
- catch (BadQuery &er)
- {
- return -1;
- }
- catch(BadConversion &er)
- {
- return -1;
- }
- catch (Exception &er)
- {
- return -1;
- }
- }
- bool CDataBase::Excute(string strSQL)
- {
- try
- {
- if (m_ConnObject.connected())
- {
- //if (strSQL.find("select") != -1)
- {
- Query query=m_ConnObject.query();
- query<<strSQL;
- //StoreQueryResult tmp=
- query.store();
- }
- return true;
- }
- else
- {
- return false;
- }
- }
- catch (BadQuery &er)
- {
- string str=er.what();
- str="Wrong Operation:"+str;
- AfxMessageBox(str.c_str());
- return false;
- }
- catch (BadConversion &er)
- {
- return false;
- }
- catch (Exception &er)
- {
- return false;
- }
- }
- bool CDataBase::AutoSimpleQuery(string strSQL,UseQueryResult &res)
- {
- try
- {
- Query query=m_ConnObject.query();
- query<<strSQL;
- res=query.use();
- return true;
- }
- catch (BadQuery &er)
- {
- return false;
- }
- }
- void CDataBase::Close()
- {
- if (m_ConnObject.connected())
- {
- m_ConnObject.disconnect();
- }
- }
- bool CDataBase::FileToBuffer(string path,string &data)
- {
- try
- {
- char* buf = new char [1024*1024*5];//基本缓冲区5M
- int fleng;//文件长度
- int realleng;
- FILE* fp;
- fp=fopen(path.c_str (),"rb");
- if (fp!=NULL)
- {
- fseek(fp,0,SEEK_END );
- fleng=ftell(fp);
- fseek(fp,0,SEEK_SET);
- realleng=(int)fread(buf,sizeof(char),fleng,fp);
- data.assign(reinterpret_cast<char *>(buf),realleng);
- fclose(fp);
- delete [ ]buf;
- return true;
- }
- else
- {
- delete [ ]buf;
- return false;
- }
- }
- catch (std::exception &er)
- {
- return false;
- }
- }
- bool CDataBase::ReadBLOBFile(string table,string attname, string queryCondition,string path)
- {
- try
- {
- Query query = m_ConnObject.query();
- query<<"select "<<attname<<" from "<<table<<" "<<queryCondition;
- UseQueryResult res=query.use( );
- Row row=res.fetch_row();
- if (!row.empty())
- {
- ofstream tmpout(path.c_str());
- tmpout.close();
- ofstream fout(path.c_str(),ios_base::binary);
- fout.write(row[0],row[0].length());
- fout.close();
- return true;
- }
- else
- {
- return false;
- }
- }
- catch (BadQuery& er)
- {
- return false;
- }
- catch(Exception &er)
- {
- return false;
- }
- }
- bool CDataBase::ReadBLOBFile(string table,string attname, string queryCondition,Row &row)
- {
- try
- {
- Query query = m_ConnObject.query();
- query<<"select "<<attname<<" from "<<table<<" "<<queryCondition;
- UseQueryResult res=query.use( );
- row=res.fetch_row();
- if (!row.empty())
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- catch (BadQuery& er)
- {
- return false;
- }
- catch(Exception &er)
- {
- return false;
- }
- }
- bool CDataBase::ManualGetTableContent(string strSQL,StoreQueryResult &res)
- {
- try
- {
- if (m_ConnObject.connected())
- {
- Query query=m_ConnObject.query();
- query<<strSQL;
- res=query.store();
- return true;
- }
- else
- {
- cerr<<"Error: database has not be connected"<<endl;
- return false;
- }
- }
- catch (BadQuery &er)
- {
- return false;
- }
- catch(Exception &er)
- {
- return false;
- }
- }
- int CDataBase::AutoCount(string strSQL)
- {
- Open();
- try
- {
- Query query=m_ConnObject.query(strSQL);
- StoreQueryResult res=query.store();
- int num=(int)res.num_rows();
- query.reset();
- Close();
- return num;
- }
- catch (BadQuery &er)
- {
- return -1;
- }
- }
- int CDataBase::ManualCount(string strSQL)
- {
- try
- {
- Query query=m_ConnObject.query(strSQL);
- StoreQueryResult res=query.store();
- return (int)res.num_rows();
- }
- catch (BadQuery &er)
- {
- return -1;
- }
- catch (Exception &er)
- {
- return -1;
- }
- }
- CDataBase::~CDataBase(void)
- {
- if (m_ConnObject.connected())
- {
- delete m_pTrans;
- m_ConnObject.disconnect();
- }
- }