mysqlc++程序代码_C++代码操作MYSQL
原创作品,允许转载,转载时请务必以超链接形式标明⽂章、原创出处 、作者信息和本声明。否则将追究法律责任。
⾸先废话⼀下...这是⼩弟的第⼀篇⽂章...写的不好多多包涵啊....
第⼀步:咱们的电脑必须要安装⼀个MYSQL的服务...⾄于如何安装...不懂得⾃⼰去度娘吧!~安装教程
第⼆步:有了个MYSQL的服务器之后,偶就装了个Navicat作为客户端⽤来可视化操作下载地址也是
(主要⽤于创建MYSQL数据库,设计表结构...当我们完成这⼀步就可以进⼊主题了)
第三步:确保你的电脑已经安装上了VS或别的本⼈⽤的是VS来写的C++....此时我们需要配置好VS引⽤的MYSQL的库
此时...我们的准备⼯作就绪了....本⼈VS2010
第四步:添加我们的MYSQL操作的⾃定义类...很简单的类,⼤家别见笑了
头⽂件如下:
//CMySql.h
#include
#include "mysql.h"
#include "string"
//#define Max_SQL_Query_Len 2048
//返回值
typedef enum _enumResCode
{
NotSetResCode = 0,
Success,
InitDBFail,
ConnDBFail,
SetCharacterFail,
ExecuteQueryFail,
}_enResCode;
//class string;
class CMysql
{
public:
//变量
MYSQL m_kMysql;
/
/构造函数和稀构函数
CMysql();
mysql下载的vs库放在那个文件里~CMysql();
int ConnMySQL(const char *host,
const char * port ,
const char * Db,
const char * user,
const char* passwd,
const char * charset,
std::string &Msg);
std::string SelectData(char * SQL,std::string& Msg , unsigned __int64* pun64AffectCow = NULL); int ExecuteNotResultSQL(char * SQL,std::string& Msg);
void CloseMySQLConn();
};
实现的CPP如下:
//CMySql.cpp
#include
#include
#include "CMySql.h"
#include
using namespace std;
CMysql::CMysql()
{
}
CMysql::~CMysql()
{
CloseMySQLConn();
}
//初始化数据
int CMysql::ConnMySQL(const char *host,
const char * port ,
const char * Db,
const char * user,
const char* passwd,
const char * charset,
string &Msg)
{
int nRes = Success;
try
{
//init MySql
if( mysql_init(&m_kMysql) == NULL )
{
nRes = InitDBFail;
}
else
{
//conncet DB
if (mysql_real_connect(&m_kMysql,host,user,passwd,Db,0,NULL,0) == NULL) {
nRes = ConnDBFail;
}
else
{
//set show character
if(mysql_set_character_set(&m_kMysql,/*"GBK"*/charset) != 0)
{
nRes = SetCharacterFail;
}
else
{
Msg = "Connect DB success!\n";
}
}
}
}
catch (...)
{
/
/todo:write log mark DB Return Error MSG
Msg = mysql_error(&m_kMysql);
}
return nRes;
}
//查询数据
string CMysql::SelectData(char * SQL,string& Msg,unsigned __int64* pun64AffectCow/* = NULL*/) {
MYSQL_ROW Row;
MYSQL_RES *pRes;
string strRes;
string sql = SQL;
char rg = ' ';//TODO:暂时⽤空格隔开,优化则需要存⼊⼀个容器
try
{
if(!pty())
{
//execute query
if(mysql_query(&m_kMysql,sql.c_str()) != 0)
{
//write log,query Fail
}
else//query success
{
Msg = "query success!\n";
pRes = mysql_store_result(&m_kMysql);
if(NULL == pRes)
{
//write log,res NULL
}
else
{
//get res to string
unsigned __int64 un64RowNums = mysql_num_rows(pRes);
unsigned int unCnums = mysql_num_fields(pRes);
for(unsigned __int64 un64Num_rows = 0; un64Num_rows < un64RowNums; ++un64Num_rows ) {
Row = mysql_fetch_row(pRes);
for(unsigned int unNum_fields = 0; unNum_fields < unCnums; ++unNum_fields)
{
strRes += Row[unNum_fields];
strRes += rg;
}
}
//受影响的⾏数
if(pun64AffectCow)
{
*pun64AffectCow = mysql_affected_rows(&m_kMysql);
}
}
//free result
mysql_free_result(pRes);
}
}
}
catch(...)
{
/
/todo:write log mark DB Return Error MSG
Msg = mysql_error(&m_kMysql);
}
return strRes;
}
//执⾏不需要返回记录集的SQL
int CMysql::ExecuteNotResultSQL(char * SQL,string& Msg)
{
string sql = SQL;
int nRes = Success;
if(!pty())
{
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论