MySQL++编程

封装MySQL++类,实现一些数据库基本操作,还包括大二进制文件的读写。

  1. #include "StdAfx.h"
  2. #include "DataBase.h"
  3. #include <string>
  4. #include <iostream>
  5. #include <fstream>
  6. using namespace std;
  7. using namespace mysqlpp;
  8. CDataBase::CDataBase( )
  9. {
  10. }
  11. void CDataBase::SetConnectionString(string dataBaseName,string serverIP, string userName,string userPassword)
  12. {
  13.     m_DBname=dataBaseName;
  14.     m_ServerIP=serverIP;
  15.     m_UserName=userName;
  16.     m_Password=userPassword;
  17. }
  18. bool CDataBase::Open()
  19. {
  20.     try
  21.     {
  22.         m_ConnObject.connect(m_DBname.c_str(),m_ServerIP.c_str(),m_UserName.c_str(),m_Password.c_str());
  23.         return true;
  24.     }
  25.     catch (BadOption &er)
  26.     {
  27.         return false;
  28.     }
  29.     catch (ConnectionFailed &er)
  30.     {
  31.         return false;
  32.     }
  33.     catch (Exception &er)
  34.     {
  35.         return false;
  36.     }
  37. }
  38. void CDataBase::BeginTransaction()
  39. {
  40.     try
  41.     {
  42.         if (m_ConnObject.connected())
  43.         {
  44.             m_pTrans = new Transaction(m_ConnObject);//nodelete
  45.         }
  46.     }
  47.     catch (Exception &er)
  48.     {
  49.         
  50.     }
  51. }
  52. void CDataBase::Commit()
  53. {
  54.     try
  55.     {
  56.         if (m_ConnObject.connected())
  57.         {
  58.             m_pTrans->commit(); 
  59.             delete m_pTrans;
  60.         }   
  61.     }
  62.     catch (Exception &er)
  63.     {
  64.     }
  65. }
  66. void CDataBase::Rollback()
  67. {
  68.     try
  69.     {
  70.         if (m_ConnObject.connected())
  71.         {
  72.             m_pTrans->rollback();   
  73.             delete m_pTrans;
  74.         }
  75.     }
  76.     catch (Exception &er)
  77.     {
  78.     }
  79. }
  80. int CDataBase::MaxID(string table,string idname)
  81. {
  82.     int maxid=0;
  83.     try
  84.     {
  85.         Query query=m_ConnObject.query();
  86.         query<<"select max("<<idname<<") from "<<table;
  87.         //query<<"select max(pId) from part";
  88.         if(UseQueryResult res=query.use())
  89.         {
  90.             Row row=res.fetch_row();
  91.             if (row[0])
  92.             {
  93.                 maxid=row[0];
  94.             } 
  95.             else
  96.             {   
  97.                 maxid=0;
  98.             }
  99.         }
  100.         else
  101.         {
  102.             maxid=0;
  103.         }
  104.         return maxid;
  105.     }
  106.     catch (BadQuery &er) 
  107.     {
  108.         return -1;
  109.     }
  110.     catch(BadConversion &er)
  111.     {
  112.         return -1;
  113.     }
  114.     catch (Exception &er)
  115.     {
  116.         return -1;
  117.     }
  118. }
  119. bool CDataBase::Excute(string strSQL)
  120. {
  121.     try
  122.     {
  123.         if (m_ConnObject.connected())
  124.         {
  125.             //if (strSQL.find("select") != -1)
  126.             {
  127.                 Query query=m_ConnObject.query();
  128.                 query<<strSQL;
  129.                 //StoreQueryResult tmp=
  130.                 query.store();
  131.             } 
  132.             return true;
  133.         }
  134.         else
  135.         {
  136.             return false;
  137.         }
  138.     }
  139.     catch (BadQuery &er)
  140.     {
  141.         string str=er.what();
  142.         str="Wrong Operation:"+str;
  143.         AfxMessageBox(str.c_str());
  144.         return false;
  145.     }
  146.     catch (BadConversion &er)
  147.     {
  148.         return false;
  149.     }
  150.     catch (Exception &er)
  151.     {
  152.         return false;
  153.     }
  154. }
  155. bool CDataBase::AutoSimpleQuery(string strSQL,UseQueryResult &res)
  156. {
  157.     try
  158.     {
  159.         Query query=m_ConnObject.query();
  160.         query<<strSQL;
  161.         res=query.use();
  162.         return true;
  163.     }
  164.     catch (BadQuery &er)
  165.     {
  166.         return false;
  167.     }   
  168. }
  169. void CDataBase::Close()
  170. {
  171.     if (m_ConnObject.connected())
  172.     {
  173.         m_ConnObject.disconnect();
  174.     }
  175. }
  176. bool CDataBase::FileToBuffer(string path,string &data)
  177. {
  178.     try
  179.     {
  180.         char* buf = new char [1024*1024*5];//基本缓冲区5M
  181.         int fleng;//文件长度
  182.         int realleng;
  183.         FILE* fp;
  184.         fp=fopen(path.c_str (),"rb");
  185.         if (fp!=NULL)
  186.         {
  187.             fseek(fp,0,SEEK_END );
  188.             fleng=ftell(fp);
  189.             fseek(fp,0,SEEK_SET);
  190.             realleng=(int)fread(buf,sizeof(char),fleng,fp);
  191.             data.assign(reinterpret_cast<char *>(buf),realleng);
  192.             fclose(fp);
  193.             delete [ ]buf;
  194.             return true;
  195.         }
  196.         else
  197.         {
  198.             delete [ ]buf;
  199.             return false;
  200.         }
  201.     }
  202.     catch (std::exception &er)
  203.     {
  204.         return false;
  205.     }
  206. }
  207. bool CDataBase::ReadBLOBFile(string table,string attname, string queryCondition,string path)
  208. {
  209.     try
  210.     {
  211.         Query query = m_ConnObject.query();
  212.         query<<"select "<<attname<<" from "<<table<<" "<<queryCondition;
  213.         UseQueryResult res=query.use( );
  214.         Row row=res.fetch_row();
  215.         if (!row.empty())
  216.         {
  217.             ofstream tmpout(path.c_str());
  218.             tmpout.close();
  219.             ofstream fout(path.c_str(),ios_base::binary);
  220.             fout.write(row[0],row[0].length());
  221.             fout.close();
  222.             return true;
  223.         } 
  224.         else
  225.         {
  226.             return false;
  227.         }
  228.     }
  229.     catch (BadQuery& er)
  230.     {
  231.         return false;
  232.     }
  233.     catch(Exception &er)
  234.     {
  235.         return false;
  236.     }
  237. }
  238. bool CDataBase::ReadBLOBFile(string table,string attname, string queryCondition,Row &row)
  239. {
  240.     try
  241.     {
  242.         Query query = m_ConnObject.query();
  243.         query<<"select "<<attname<<" from "<<table<<" "<<queryCondition;
  244.         UseQueryResult res=query.use( );
  245.         row=res.fetch_row();
  246.         if (!row.empty())
  247.         {
  248.             return true;
  249.         } 
  250.         else
  251.         {
  252.             return false;
  253.         }
  254.     }
  255.     catch (BadQuery& er)
  256.     {
  257.         return false;
  258.     }
  259.     catch(Exception &er)
  260.     {
  261.         return false;
  262.     }
  263. }
  264. bool CDataBase::ManualGetTableContent(string strSQL,StoreQueryResult &res)
  265. {
  266.     try
  267.     {
  268.         if (m_ConnObject.connected())
  269.         {
  270.             Query query=m_ConnObject.query();
  271.             query<<strSQL;
  272.             res=query.store();
  273.             return true;
  274.         } 
  275.         else
  276.         {
  277.             cerr<<"Error: database has not be connected"<<endl;
  278.             return false;
  279.         }
  280.     }
  281.     catch (BadQuery &er)
  282.     {
  283.         return false;
  284.     }   
  285.     catch(Exception &er)
  286.     {
  287.         return false;
  288.     }
  289. }
  290. int  CDataBase::AutoCount(string strSQL)
  291. {
  292.     Open();
  293.     try
  294.     {
  295.         Query query=m_ConnObject.query(strSQL); 
  296.         StoreQueryResult res=query.store();
  297.         int num=(int)res.num_rows();
  298.         query.reset();
  299.         Close();
  300.         return num;
  301.     }
  302.     catch (BadQuery &er)
  303.     {
  304.         return -1;
  305.     }
  306. }
  307. int  CDataBase::ManualCount(string strSQL)
  308. {
  309.     try
  310.     {
  311.         Query query=m_ConnObject.query(strSQL);
  312.         StoreQueryResult res=query.store();
  313.         return (int)res.num_rows();
  314.     }
  315.     catch (BadQuery &er)
  316.     {
  317.         return -1;
  318.     }
  319.     catch (Exception &er)
  320.     {
  321.         return -1;
  322.     }
  323. }
  324. CDataBase::~CDataBase(void)
  325. {
  326.     if (m_ConnObject.connected())
  327.     {
  328.         delete m_pTrans;
  329.         m_ConnObject.disconnect();
  330.     }
  331. }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值