//包含四个主要的类
/*
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;
}
}
/*
主要功能:将二进制的数据
MySQL C++ 封装类
最新推荐文章于 2024-08-30 05:00:00 发布
本文详细介绍了如何使用C++对MySQL数据库进行接口封装,包括连接管理、SQL语句执行、结果集处理等关键步骤,旨在提供一种高效、稳定的数据库操作方式。
摘要由CSDN通过智能技术生成