程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> 關於MYSQL數據庫 >> 一個Mysql C API小型封裝

一個Mysql C API小型封裝

編輯:關於MYSQL數據庫

主要用於維護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);
        }

}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved