| 
          ">
           
            //DBMysql.h文件
           
          
            #ifndef DB_MYSQL_H 
            #define DB_MYSQL_H 
            #include <winsock2.h> 
            #include <mysql.h> 
            #include <string> 
            #include <map> 
            #include <vector> 
            #include <iostream> 
            using namespace std; 
            typedef map<string,string > strMap;
           
          
            /*mysql操作类,封装了c语言相关的api,可实现基本的查询、插入、修改和删除动作*/ 
            class DBMysql 
            { 
            protected: 
             MYSQL *mysql; //代表一个到数据库的连接 
            private: 
             string host; //连接的服务器 
             string user; //用户名 
             string password; //连接密码 
             unsigned int port; //连接端口 
             string db; //操作的数据库的名称 
             MYSQL_RES *result; //操作的结果 
             string query; //sql语句 
             unsigned long num; //返回查询得到的结果数 
             string error; //错误提示信息 
             unsigned int debug; //是否显示调试信息 
             strMap info ; //查询语句返回一条结果 
             vector<strMap> arrInfo; //查询语句可能会返回多条结果 
             vector<string> fields; //返回查询结果的列 
             void disPlayError(); 
            public: 
             DBMysql(string host,string user,string password,unsigned int port);// 构造函数 
             DBMysql(); //构造函数 
             void SetConnect(string host,string user,string password,unsigned int port);//确定连接参数 
             unsigned int DBConnect();//连接数据库 
             unsigned int DBSelect(string db); //连接一个数据库 
             void SetQuery(string q); //设定查询语句 
             unsigned int DBQuery(); //查询数据库 
             strMap GetInfo(); //返回查询得到的一条结果 
             vector<strMap> GetArray(); //返回查询得到的结果 
             string GetError(); //返回错误信息 
             vector<string> GetFields();//返回查询后的列值 
             unsigned int InsertData(string table,strMap *data); //向数据库中插入一条数据 
             unsigned long GetLastID(); //返回最后一个自动增量的值 
             unsigned long GetNum(); //返回一条sql语句影响的行数 
             unsigned int UpdateData(string table,strMap *data,string condition); //根据条件修改一条数据 
             unsigned int DeleteData(string table,string condition); //根据条件删除数据 
             ~DBMysql();//析构函数 
            }; 
            #endif
           
          
             
             //DBMysql.cpp文件
           
          
            #include "DBMysql.h" 
            #include <assert.h> 
            /*构造函数,设定连接的服务器,用户名,密码和端口*/ 
            DBMysql::DBMysql(string host,string user,string password,unsigned int port=3306) 
            { 
             mysql = mysql_init(NULL); 
             num = 0; 
             error=""; 
             query=""; 
             result = NULL; 
             SetConnect(host,user,password,port); 
            } 
            DBMysql::DBMysql() 
            { 
            } 
            /*设定连接的服务器,用户名,密码和端口*/ 
            void DBMysql::SetConnect(string host,string user,string password,unsigned int port) 
            { 
             DBMysql::host = host; 
             DBMysql::user = user; 
             DBMysql::password = password; 
             DBMysql::port = port; 
            } 
            /*连接数据库*/ 
            unsigned int DBMysql::DBConnect() 
            { 
             MYSQL *con; 
             if(mysql == NULL) 
             { 
              error = "初始化mysql错误"; 
              return 1; 
             } 
             con = mysql_real_connect(mysql,host.c_str(),user.c_str(),password.c_str(),NULL,port,NULL,0); 
             if(con == NULL) 
             { 
              error=mysql_error(mysql); 
              return mysql_errno(mysql); 
             } 
             return 0; 
            } 
            /*选择一个数据库*/ 
            unsigned int DBMysql::DBSelect(string database) 
            { 
             unsigned int re; 
             if( mysql == NULL) return 1; 
             db = database; 
             re = mysql_select_db(mysql,db.c_str()); 
             if(re != 0) 
             { 
              error+=mysql_error(mysql); 
             } 
             return re; 
            } 
            /*设定sql语句*/ 
            void DBMysql::SetQuery(string q) 
            {
           
          
             assert(!q.empty()); 
             if(result != NULL ) 
             { 
              mysql_free_result(result); 
             } 
             query = q; 
            } 
            /*执行sql语句*/ 
            unsigned int DBMysql::DBQuery() 
            { 
             unsigned int re; 
             if( mysql == NULL) return 1; 
             assert(!query.empty()); 
             re = mysql_query(mysql,query.c_str()); 
             if(re == 0) 
             { 
              result = mysql_store_result(mysql); 
              num = mysql_affected_rows(mysql); 
              info.clear(); 
              arrInfo.clear(); 
              fields.clear(); 
             } 
             else 
             { 
              re = mysql_errno(mysql); 
              error = mysql_error(mysql); 
              cout<<error<<endl; 
             } 
             return re; 
            }
           
          
             
            /*获取查询得到的一条结果*/ 
            strMap DBMysql::GetInfo() 
            { 
             MYSQL_ROW row; 
             unsigned int i; 
             assert(mysql != NULL); 
             if(info.size() > 0) return info; 
             if(result != NULL) 
             { 
              GetFields(); 
              row = mysql_fetch_row(result); 
              if(row != NULL) 
              { 
               for(i=0;i<fields.size();i++) 
               { 
                info[fields[i]] = (char*)row[i]; 
               } 
              } 
              //mysql_free_result(result); 
             } 
             return info; 
            } 
            /*获取查询得到的所有结果*/ 
            vector<strMap> DBMysql::GetArray() 
            { 
             MYSQL_ROW row; 
             unsigned int i; 
             strMap tmp; 
             assert(mysql != NULL); 
             if(arrInfo.size() > 0) return arrInfo; 
             if(result != NULL) 
             { 
              GetFields(); 
              while(row = mysql_fetch_row(result)) 
              { 
               if(row != NULL) 
               { 
                for(i=0;i<fields.size();i++) 
                { 
                 tmp[fields[i]] = (char *)row[i]; 
                } 
                arrInfo.push_back(tmp); 
               } 
              } 
             } 
             return arrInfo; 
            } 
            /*获取sql语句执行影响的行数*/ 
            unsigned long DBMysql::GetNum() 
            { 
             return num; 
            } 
            /*获取插入后的id号*/ 
            unsigned long DBMysql::GetLastID() 
            { 
             return mysql_insert_id(mysql); 
            } 
            /*向数据库插入数据*/ 
            unsigned int DBMysql::InsertData(string table,strMap *data) 
            { 
             strMap::const_iterator iter; 
             string q1; 
             int flag=0; 
             assert(mysql != NULL); 
             assert(!table.empty()); 
             assert(data != NULL); 
             for(iter = data->begin();iter!= data->end();iter++) 
             { 
              if(flag == 0) 
              { 
               q1 = "insert into "; 
               q1+=table; 
               q1+=" set "; 
               q1+=iter->first; 
               q1+="=''''"; 
               q1+=iter->second; 
               q1+="''''"; 
               flag++; 
              } 
              else 
              { 
               q1+=","; 
               q1+=iter->first; 
               q1+="=''''"; 
               q1+=iter->second; 
               q1+="''''"; 
              } 
             } 
             SetQuery(q1); 
             return DBQuery(); 
            } 
            /*根据条件修改数据*/ 
            unsigned int DBMysql::UpdateData(string table,strMap *data,string condition) 
            { 
             strMap::const_iterator iter; 
             string q1; 
             int flag=0; 
             assert(mysql != NULL); 
             assert(!table.empty()); 
             assert(data != NULL); 
             for(iter = data->begin();iter!= data->end();iter++) 
             { 
              if(flag == 0) 
              { 
               q1 = "update "; 
               q1+=table; 
               q1+=" set "; 
               q1+=iter->first; 
               q1+="=''''"; 
               q1+=iter->second; 
               q1+="''''"; 
               flag++; 
              } 
              else 
              { 
               q1+=","; 
               q1+=iter->first; 
               q1+="=''''"; 
               q1+=iter->second; 
               q1+="''''"; 
              } 
             } 
             if(!condition.empty()) 
             { 
              q1+=" where "; 
              q1+=condition; 
             } 
             SetQuery(q1); 
             return DBQuery(); 
            } 
            /*根据条件删除数据*/ 
            unsigned int DBMysql::DeleteData(string table,string condition) 
            { 
             string q; 
             assert(mysql != NULL); 
             assert(!table.empty()); 
             q="delete from "; 
             q+=table; 
             if(!condition.empty()) 
             { 
              q+=" where "; 
              q+=condition; 
             } 
             SetQuery(q); 
             return DBQuery(); 
            }
           
          
            /*获取返回的错误信息*/ 
            string DBMysql::GetError() 
            { 
             return error; 
            }
           
          
            /*返回查询后的列值*/ 
            vector<string> DBMysql::GetFields() 
            { 
             /* 
              field = mysql_fetch_fields(result); 
              然后通过field[i].name访问在此有错误,不知道为什么,可能是mysql的bug 
             */ 
             MYSQL_FIELD *field; 
             assert(mysql != NULL); 
             if(fields.size()>0) return fields; 
             while(field = mysql_fetch_field(result)) 
             { 
              fields.push_back(field->name); 
             } 
             return fields; 
            }
           
          
            /*析构函数*/ 
            DBMysql::~DBMysql() 
            { 
             if(result != NULL) 
              mysql_free_result(result); 
             fields.clear(); 
             error=""; 
             info.clear(); 
             db=""; 
             arrInfo.clear(); 
             mysql_close(mysql); 
            }
           
          
            //例子test.php
           
          
            #include "DBMysql.h" 
            #include <string> 
            #include <vector> 
            using namespace std; 
            int main() 
            { 
             vector<strMap> info; 
             DBMysql db("localhost","root",""); 
             db.DBConnect(); 
             db.DBSelect("mysql"); 
             string query = "select user,password from user"; 
             db.SetQuery(query); 
             db.DBQuery(); 
             info = db.GetArray(); 
             for(int i=0;i<info.size();i++) 
             { 
              cout<<info[i]["user"]<<":"<<info[i]["password"]<<endl; 
             } 
             return 1; 
            }
           
          
            //呵呵,希望大家能够喜欢,有bug或改进意见希望告诉我。
           
         |