MySQL C++ 封装类

本文详细介绍了如何使用C++对MySQL数据库进行接口封装,包括连接管理、SQL语句执行、结果集处理等关键步骤,旨在提供一种高效、稳定的数据库操作方式。
摘要由CSDN通过智能技术生成
//包含四个主要的类
/*
 1. K_Database  数据库
 2. k_Record    单条记录
 3. k_RecordSet 记录集合
 4. k_Field     字段信息

 K_mysql.h

 */
#ifdef WIN32
#include <winsock.h>
#else
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#endif

#include <string>
#include "mysql.h"
#include <vector>
using namespace std;


class k_sqlfactory
{
public:
 k_sqlfactory()
 {
  clean();
 }
 vector<string> name_list;
 vector<string> value_list;

 string where;
 string tablename;

 void clean()
 {
  name_list.clear();
  value_list.clear();
  where = "";
  tablename = "";
 };

 /*
  主要功能:插入新的记录
  返回值:0成功 -1 执行查询失败
  说明:以来 m_value m_field 提供的信息 生成sql 语句再查询
  */
 void add_value(string newname, string newvalue)
 {
  name_list.push_back(newname);
  value_list.push_back(newvalue);
 };
 string get_add()
 {

  if ((tablename.c_str() == "") || (this->tablename.empty()))
   return "-1";
  string sqltext = "INSERT INTO ";
  sqltext += tablename;
  sqltext += "(";
  int j = (int) this->name_list.size();
  int i;
  for (i = 0; i < j; i++)
  {

   sqltext += name_list[i];
   if (i < j - 1)
    sqltext += ",";
  }
  sqltext += ") VALUES (";
  j = (int) value_list.size();
  for (i = 0; i < j; i++)
  {
   sqltext += "'";
   sqltext += value_list[i];
   sqltext += "'";

   if (i < j - 1)
    sqltext += ",";

  }
  sqltext += ")";
  return sqltext;
 }
 string get_edit()
 {
  if ((tablename.c_str() == "") || (this->tablename.empty())
    || (this->where.c_str() == "") || (this->where.empty()))
   return "-1";
  string sqltext = "UPDATE ";
  sqltext += tablename;
  sqltext += " SET ";
  int j = (int) this->name_list.size();
  for (int i = 0; i < j; i++)
  {

   sqltext += name_list[i];
   sqltext += "='";
   sqltext += value_list[i];
   sqltext += "'";

   if (i < j - 1)
    sqltext += ",";

  }
  if (sqltext.find_last_of(",") == sqltext.length())
   sqltext.erase(sqltext.end());

  sqltext += " where (";
  sqltext += where;
  sqltext += ")";

  return sqltext;

 }
 /*
  主要功能:编辑制定的记录
  返回值:0成功 -1 执行查询失败
  说明:删除 m_where制定的条件 的记录 生成sql 语句再查询
  */
 string get_del()
 {
  string sqltext;

  if ((tablename.c_str() == "") || (this->tablename.empty())
    || (this->where.c_str() == "") || (this->where.empty()))
   return "-1";
  else
  {
   sqltext = "Delete  from ";
   sqltext += tablename;
   sqltext += " Where ";
   sqltext += where;

   return sqltext;

  }

 }
 /*
  主要功能:将二进制的数据
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值