(4). 数据库操作类的实现(dbhelper.cpp)源码
 
#include"include/dbhelper.h"

DbHelper::DbHelper(char *user,char *pass,char *server,unsigned int port,char *defaultDb,
        char *socket,unsigned int flag)
{
        connect_arg=new Fields(user,pass,port,server,defaultDb,socket,flag);
        Connect();
}
DbHelper::DbHelper(Fields &conf)
{
        connect_arg=new Fields(conf);
        Connect();
}
DbHelper::~DbHelper()
{
        delete connect_arg;
        DisConnect();
}
void DbHelper::DisConnect()
{
        mysql_close(db);
        freeResult();
        setIsInit(false);
        setIsConn(false);
}
void DbHelper::Init()
{
        db=mysql_init(NULL);
        if(db==NULL) //初始化失败
                setIsInit(false);
        else //初始化成功
                setIsInit(true);
}
 
void DbHelper::Connect()
{
        Init();
        if(getIsInit())
        {
                if(mysql_real_connect(db,connect_arg->getServer(),connect_arg->getUser(),
                        connect_arg->getPass(),connect_arg->getDefaultDb(),connect_arg->getPort(),
                        connect_arg->getSocket(),connect_arg->getFlag())==NULL)
                {       //连接失败
                        setIsConn(false);
                }
                else
                {
                        //连接成功
                        setIsConn(true);
                }
        }
        else
                setIsConn(false); //设置连接失败
}
 
void DbHelper::ReConnect()
{
        if(getIsConn())
                DisConnect();
        Connect();
}
 
bool DbHelper::execSQL(char *sql)
{
        if(getIsConn())
        {
                freeResult();
                if(mysql_real_query(db,sql,strlen(sql))==0)
                {
                        MYSQL_RES *res=mysql_store_result(db);
                        if(res==NULL)
                        {
                                //无结果集
                                setHasResults(false);
                        }
                        else
                        {
                                //有结果集
                                setHasResults(true);
                                MYSQL_ROW row;
                                while((row=mysql_fetch_row(res))!=NULL)
                                {
                                        result.push_back(row);
                                }
                                MYSQL_FIELD *tmpfield;
                                num_fields=mysql_num_fields(res);
                                for(int fi=0;fi<num_fields;fi++)
                                        fields.push_back(make_pair(mysql_fetch_field_direct(res,fi),fi));
                        }
                        mysql_free_result(res);
                        return true;
                }
                else
                { //执行失败
                        return true;
                }
        }
        else
        {
                return false;
        }
}
 
void DbHelper::show()
{
        if(hasResults())
        {
                for(ResultType::iterator iter=result.begin();iter != result.end();iter++)
                {
                        for(int i=0;i<num_fields;i++)
                                std::cout<<(*iter)[i]<<"  | \t";
                        std::cout<<std::endl;
                }
        }
        else
        {
                //无结果集
                std::cout<<"无结果集显示\n";
        }
}
void DbHelper::showField()
{
        int len=0;
        for(FieldType::iterator iter=fields.begin();iter!=fields.end();iter++)
        {
                cout<<iter->first->name<<" | \t";
                len+=string(iter->first->name).size()+string(" | ").size()+8;
        }
        cout<<endl;
        for(int i=0;i<len;i++)
                cout<<"-";
        cout<<endl;
}
void DbHelper::showList(ListType::iterator beg,ListType::iterator end)
{
        while(beg !=end)
        {
                cout<<*beg<<endl;
                beg++;
        }
}
 
ostream & operator<<(ostream &os,const DbHelper& r)
{
        if(r.hasResults())
        {
                ResultType result=r.getResults();
                unsigned int num_fields=r.getFieldNums();
                for(ResultType::iterator iter=result.begin();iter !=result.end();iter++)
                {
                        for(int i=0;i<num_fields;i++)
                        {
                                os<<(*iter)[i]<<"  |  \t";
                        }
                        os<<endl;
                }
        }
        return os;
}
void DbHelper::freeResult()
{
        result.clear();
        fields.clear();
}

/*指定使用的数据库*/
bool DbHelper::useDb(char *database)
{
        if(strlen(database)==0)
                return false;
        else
        {
                char *sql=new char[strlen(database)+1+4];
                strcat(sql,"USE ");
                strcat(sql,database);
                bool issuccess=execSQL(sql);
                delete[] sql;
                return issuccess;
        }
}
/*获取当前数据库名*/
char * DbHelper::currentDb()
{
        char *sql="SELECT DATABASE();";
        if(execSQL(sql))
        {
                ResultType r=getResults();
                return r[0][0];
        }
        else
        {
                return NULL;
        }
}