用于操作mysql数据库的c++类

本文介绍了一个使用C++封装的MySQL操作类,该类简化了数据库的基本操作,如查询、插入、更新和删除等。提供了详细的类定义及成员函数说明,并附带了一个简单的示例程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//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或改进意见希望告诉我。


 

【来源】

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值