用于操作mysql数据库的c 类

   来历:fangshi168775的 Blog 作者:fangshi168775的 Blog  




">

//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,可完成根蒂的盘问、拔出、修正和删除举动*/
>{
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或刷新定见盼愿照顾我。




版权声明: 原创作品,许可转载,转载时请务必以超链接方式标明文章 原始理由 、作者信息和本声明。否则将追查规则责任。

原文地址:https://www.cnblogs.com/zgqjymx/p/1975720.html