概述
#include "CMYSQL.h"#include"stdafx.h"CCONMYSQL::CCONMYSQL()
{//Init lib
if (0 == mysql_library_init(0, NULL, NULL)) {//cout << "初始化库成功" << endl;
}else{//cout << "初始化库失败" << endl;
throw "初始化库失败";
}//init MYSQLDATA;
if (NULL != mysql_init(&mydata)) {//cout << "MySQL初始化成功" << endl;
}else{//cout << "MySQL初始化失败" << endl;
throw "MySQL初始化失败";
}//下面是 在连接数据库之前,设置额外的连接选项
if (0 == mysql_options(&mydata, MYSQL_SET_CHARSET_NAME, "utf8")) { //可以设置的选项很多,这里设置字符集,否则无法处理中文//cout << "MySQL设置成功" << endl;
}else{//cout << "MySQL设置失败" << endl;
throw "MySQL设置失败";
}
}
CCONMYSQL::~CCONMYSQL()
{
}void CCONMYSQL::InitConnectInfor(const char *serverip, const char *user, const char *passwd, const char *db, unsigned intport)
{//初始化数据库连接信息
m_ConInfo.serverip =serverip;
m_ConInfo.user=user;
m_ConInfo.password=passwd;
m_ConInfo.database=db;
m_ConInfo.port=port;
}boolCCONMYSQL::Open()
{//下面是连接数据库
if (NULL!= mysql_real_connect(&mydata, m_ConInfo.serverip, m_ConInfo.user, m_ConInfo.password, m_ConInfo.database,m_ConInfo.port, NULL, 0))
{//cout << "数据库连接成功" << endl;
return true;
}else{//cout << "数据库连接失败" << endl;
throw "数据库连接失败";return false;/*if (mysql_errno(&mydata))
{
fprintf(stderr, "(错误原因 %d: %s)n", mysql_errno(&mydata), mysql_error(&mydata));
return false;
}*/}
}bool CCONMYSQL::QueryDatabase(stringsql)
{if (mysql_query(&mydata, sql.c_str()))
{//cout << "执行查询失败 长时间未连接 连接已丢失" << mysql_error(&mydata) << endl;
throw mysql_error(&mydata);return false;
}if (!(m_query = mysql_store_result(&mydata))) //获得sql语句结束后返回的结果集
{return false;
}//printf("iRrow = %d n", m_query->row_count);//行数、列数为 m_query->field_count
if (m_query->row_count > 0)
{//cout << "真" << endl;
return true;
}else{//cout << "假" << endl;
return false;
}return true;
}string CCONMYSQL::CreateSQL(string text,string uid,string tid,stringshuxing)
{string sql1("SELECT * FROM * WHERE * = '");string sql2("' AND * ='");string sql3("' AND * = '");string sql4("' AND * LIKE '");string sql5("'