(3).数据库操作类声明(include/dbhelper.h)源码
 

#ifndef _DBHELPER_H_
#define _DBHELPER_H_
#include<iostream>
#include<map>
#include<utility>
#include<mysql.h>
#include<vector>
#include<cstring>
#include"fields.h"
#include<string>
using namespace std;
 
/*类型定义*/
typedef vector<MYSQL_ROW> ResultType;  //结果集类型
typedef vector<pair<MYSQL_FIELD *,int> >  FieldType; //字段类型
typedef vector<string> ListType; //一维类型
class DbHelper
{
private: //数据成员
        MYSQL *db; //数据库操作符
        bool isconn; //是否成功连接
        bool isinit; //是否成功初始化
        Fields *connect_arg; //连接参数
        ResultType result; //结果集
        FieldType fields; //字段列表
        unsigned int num_fields; //字段数
        bool hasresults; //是否有结果集
public: //函数
        DbHelper(char *user,char *pass,char *server="localhost",unsigned int port=3306,
                        char *defaultDb="test",char *socket=NULL,unsigned int flag=0);
        DbHelper(Fields &conf);
        DbHelper(const DbHelper &d);
        ~DbHelper();
        /*获取*/
        bool getIsConn()const{  return  isconn;}
        bool getIsInit()const{  return  isinit;}
        Fields & getConnArg()const{ return *connect_arg;}
        ResultType getResults()const{   return result;}
        FieldType getFields()const{     return fields;}
        unsigned int getFieldNums()const{       return num_fields;}
        bool hasResults()const{ return hasresults;}
        /*设置*/
        void setIsConn(bool f=false){   isconn=f;}
        void setIsInit(bool f=false){   isinit=f;}
        void setConnArg(Fields &arg){   *connect_arg=arg;}
        void setHasResults(bool f=false){       hasresults=f;}
        void Init();
        void Connect();
        void DisConnect();
         void ReConnect();
        bool execSQL(char *sql);
        void show();
        void showField();
        void showList(ListType::iterator beg,ListType::iterator end);
        void freeResult();
        bool useDb(char *database);
        char *currentDb();
        ListType listDbs();
        ListType getEachRow(ResultType::iterator beg,ResultType::iterator end);

        /*友元<<*/
        friend ostream & operator<<(ostream &os,const DbHelper &r);
};
#endif