快速业务通道

一个Mysql C API小型封装

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-03-17

主要用于维护C结构的查询和返回。

#include 

typedef        std::string        string;

struct        MySQL_parm{
        string        host;
        string        user;
        string        passWord;
        string        database;
        string        unixsock;
};
class        DBSTMT;
class        DBMysql;

class        DBSTMT{
        DBSTMT(const DBSTMT&);
        DBSTMT&        Operator=(const DBSTMT&);
        MYSQL_STMT*        stmt_;
public:
        DBSTMT(pcsz_t        query,DBMysql&        mysql);
        void        execute(){
                if(mysql_stmt_execute(stmt_))
                        throw        mysql_stmt_error(stmt_);
        }

        void        execute(MYSQL_BIND* bind){
                if(mysql_stmt_execute(stmt_))
                        throw        mysql_stmt_error(stmt_);
                if(mysql_stmt_bind_result(stmt_,bind)){
                        throw        mysql_stmt_error(stmt_);
                }
                if(mysql_stmt_store_result(stmt_))
                        throw        mysql_stmt_error(stmt_);
        }

        //void        execute(){
        //        if(mysql_stmt_execute(stmt_))
        //                throw        mysql_stmt_error(stmt_);
        //}

        void        bind(MYSQL_BIND* bind){
                if(mysql_stmt_bind_param(stmt_,bind) )
                        throw        mysql_stmt_error(stmt_);
        }

        int        fetch(){
                return        mysql_stmt_fetch(stmt_)==0;
        }
        ~DBSTMT(){
                if(stmt_){
                        mysql_stmt_close(stmt_);
                }
        }

};


class        DBMysql{
        DBMysql(const DBMysql&);
        DBMysql&operator=(const DBMysql&);
        MYSQL        *        mysqlPtr_;

        uint32_t        errno_;
PRotected:
        friend        class        DBSTMT;
        MYSQL_STMT*        _createSTMT(){
                MYSQL_STMT        *ret=mysql_stmt_init(mysqlPtr_);
                if(ret)
                        return        ret;
                errno_=mysql_errno(mysqlPtr_);
                throw        mysql_error(mysqlPtr_);
        }
public:
        const        char*        strerr(){
                return        mysql_error(mysqlPtr_);
        }
        DBMysql():mysqlPtr_(NULL){
                mysqlPtr_=mysql_init(NULL);
                if(NULL== mysqlPtr_)
                        throw        "Mysql :outof memory";
        }
        void        open(const        mysql_parm& parm){
                if(!mysql_real_connect(mysqlPtr_,
                        parm.host.c_str(),
                        parm.user.c_str(),
                        parm.password.c_str(),
                        parm.database.c_str(),
                        0,
                        parm.unixsock.c_str(),
                        0        ))
                {
                        errno_=mysql_errno(mysqlPtr_);
                        throw(mysql_error(mysqlPtr_));
                }
        }

        void        close(){
                if(mysqlPtr_)
                {
                        mysql_close(mysqlPtr_);
                        mysqlPtr_=NULL;
                }
        }

};


DBSTMT::DBSTMT(pcsz_t        query,DBMysql&        mysql):stmt_(NULL){
        stmt_=mysql._createSTMT();
        if(!stmt_)
                throw        mysql.strerr();
        if( mysql_stmt_prepare(stmt_,query,strlen(query)) )
        {
                //const char*        err=

                throw        mysql_stmt_error(stmt_);
        }

}

#define        DECL_BIND(h,n)\
class bind_##h:public        h{\
        typedef        h        parent;\
        MYSQL_BIND                _bind[n];\
        my_bool                        _is_null[n];\
        unsigned        long        _length[n];\
public:\
        bind_##h(){\
        int        i=0;\
        bzero(_bind,sizeof(_bind));


#define        BIND_BIN(x,l)\
        _bind[i].buffer_type= MYSQL_TYPE_STRING;\
        _bind[i].buffer= (char *)&(parent::x);\
        _bind[i].buffer_length= l;\
        _bind[i].is_null= _is_null+i;\
        _bind[i].length= _length+i;\
        ++i;

#define        BIND_INT(x)\
        _bind[i].buffer_type= MYSQL_TYPE_LONG;\
        _bind[i].buffer= (char *)&(parent::x);\
        _bind[i].buffer_length= 0;\
        _bind[i].is_null= _is_null+i;\
        _bind[i].length= _length+i;\
        ++i;

#define        END_BIND(h)                }\
        operator        MYSQL_BIND*(){\
        return        _bind;\
        }\
};

 

 

使用方法;

例如想查询的内容具有以下结构
struct account{
        char        user[36];
        byte        password[16];
        uint32_t        status;
        uint32_t        id;
};

//声明查询bind 结构account,参数4个
DECL_BIND(account,4)
        BIND_BIN(user,32)
        BIND_BIN(password,16)
        BIND_INT(status)
        BIND_INT(id)
END_BIND(account)

int main(){

        try{
               
                DBMysql        mysql;
                mysql_parm        parm;
                parm.host="localhost";
                parm.user="root";
                parm.password="test";
                parm.unixsock="/var/lib/mysql/mysql.sock";
                parm.database="testdb";
                mysql.open(parm);//打开数据库
               
                DBSTMT        smt("select user,password,status,id from account",mysql); //生成一个查询语法
                DBSTMT        smt1("insert into account(user,password,status) value(?,?,?)",mysql); //另外一个
        //
               
                bind_account        acc;
               


                smt.execute(acc);//执行查询,并bind返回结果到account结构               
                while(smt.fetch()){
                printf("%s %d %d\n",acc.user,acc.status,acc.id);
                       
                };

                smt1.bind(acc);//bind查询参数
                smt1.execute();//执行
               
               
               
        }catch(const char* err){
                printf("error:%s\n",err);
        }

}

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号