原创作品,允许转载,转载时请务必以超链接形式标明文章、原创出处 、作者信息和本声明。否则将追究法律责任。
首先废话一下...这是小弟的第一篇文章...写的不好多多包涵啊....
第一步:咱们的电脑必须要安装一个MYSQL的服务...至于如何安装...不懂得自己去度娘吧!~安装教程
第二步:有了个MYSQL的服务器之后,偶就装了个Navicat作为客户端用来可视化操作MYSQL...下载地址也是
(主要用于创建MYSQL数据库,设计表结构...当我们完成这一步就可以进入主题了)
第三步:确保你的电脑已经安装上了VS或别的IDE....本人用的是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();
~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(!sql.empty())
{
//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(!sql.empty())
{
try
{
if(mysql_query(&m_kMysql,sql.c_str()) != 0)
{
nRes = ExecuteQueryFail;
}
else
{
Msg = "query success!\n";
}
}
catch(...)
{
//todo:write log mark DB Return Error MSG
Msg = mysql_error(&m_kMysql);
}
}
return 0;
}
//关闭数据库连接
void CMysql::CloseMySQLConn()
{
mysql_close(&m_kMysql);
}
第5步:调用这个自定义的类来操作MYSQL了
这里的MAIN函数用到了XML来配置读入MYSQL的信息,亲们需要去下一个TINYXML2先噢...
#include "CMySql.h"
#include
#include
#include "tinyxml2/tinyxml2.h"
using namespace std;
int main(int argc , char* argv[])
{
string host;
string user;
string port;
string pwd;
string dbname;
string charset;
string Msg;//返回DB执行结果(成功OR失败相关)
//LOAD XML,如果在安卓则不能这样LOAD,要从ZIP中先取文件流
tinyxml2::XMLDocument* doc = new tinyxml2::XMLDocument();
doc->LoadFile("db_info.xml");
if(doc)
{
tinyxml2::XMLElement* Root = doc->RootElement();
if(Root)
{
tinyxml2::XMLElement* Elem = Root->FirstChildElement("data_base");
if(Elem)
{
host = Elem->Attribute("host");
user = Elem->Attribute("user");
port = Elem->Attribute("port");
pwd = Elem->Attribute("pwd");
dbname = Elem->Attribute("dbname");
charset = Elem->Attribute("charset");
}
}
}
if(doc)
{
delete doc;
doc = NULL;
}
//初始化
CMysql* pMysql = new CMysql;
pMysql->ConnMySQL(host.c_str(),
port.c_str(),
dbname.c_str(),
user.c_str(),pwd.c_str(),
charset.c_str(),
Msg);
cout<
//查询
char * SQL = "SELECT * FROM user_info";
std::string str = pMysql->SelectData(SQL,Msg);
if( str.length() > 0 )
{
cout<
}
cout<
pMysql->CloseMySQLConn();
delete pMysql;
system("pause");
return 0;
}
最后...附上DB配置的db_info.xml文件,XXXXXX的需要填写你们自己的设定...USER我这里用的是ROOT,端口也用的是默认的3306
运行结果:
夜深了....睡觉去了....写得不好,不要见笑....欢迎大家一起来讨论...