#include "stdafx.h"
#include "VspdCTOMySQL.h"
VspdCToMySQL::VspdCToMySQL()
{
}
VspdCToMySQL::~VspdCToMySQL()
{
}
//初始化数据
int VspdCToMySQL::ConnMySQL(char *host,char * port ,char * Db,char * user,char* passwd,char * charset,char * Msg)
{
if( mysql_init(&mysql) == NULL )
{
Msg = "inital mysql handle error";
return 1;
}
if (mysql_real_connect(&mysql,host,user,passwd,Db,0,NULL,0) == NULL)
{
Msg = "Failed to connect to database: Error";
return 1;
}
if(mysql_set_character_set(&mysql,"GBK") != 0)
{
Msg = "mysql_set_character_set Error";
return 1;
}
return 0;
}
//查询数据
string VspdCToMySQL::SelectData(char * SQL,char * Msg,char* DivCell,char* DivRow)
{
MYSQL_ROW m_row;
MYSQL_RES *m_res;
char sql[2048];
sprintf(sql,SQL);
int rnum = 0;
//char rg = 0x06;//行隔开
string cg(DivCell);
string rg(DivRow);//字段隔开
if(mysql_query(&mysql,sql) != 0)
{
Msg = "select ps_info Error";
return "";
}
m_res = mysql_store_result(&mysql);
if(m_res==NULL)
{
Msg = "select username Error";
return "";
}
string str("");
int CellCount = mysql_num_fields(m_res);
int RowCount = mysql_num_rows(m_res);
while(m_row = mysql_fetch_row(m_res))
{
for(int i = 0;i <CellCount ;i++)
{
str += m_row[i];
if(i<CellCount-1)str += cg;
}
if (rnum<RowCount-1)str += rg;
rnum++;
}
mysql_free_result(m_res);
return str;
}
//插入数据
int VspdCToMySQL::InsertData(char * SQL,char * Msg)
{
char sql[2048];
sprintf(sql,SQL);
if(mysql_query(&mysql,sql) != 0)
{
Msg = "Insert Data Error";
return 1;
}
return 0;
}
//更新数据
int VspdCToMySQL::UpdateData(char * SQL,char * Msg)
{
char sql[2048];
sprintf(sql,SQL);
if(mysql_query(&mysql,sql) != 0)
{
Msg = "Update Data Error";
return 0;
}
return mysql_affected_rows(&this->mysql);
}
//删除数据
int VspdCToMySQL::DeleteData(char * SQL,char * Msg)
{
char sql[2048];
sprintf(sql,SQL);
if(mysql_query(&mysql,sql) != 0)
{
Msg = "Delete Data error";
return 1;
}
return 0;
}
//关闭数据库连接
void VspdCToMySQL::CloseMySQLConn()
{
mysql_close(&mysql);
}
就是这个代码,这个是我从别的里面copy过来的,project-setting 里面要怎样改呢?怎么通过#pragam加载lib文件呢?怎么添加lib文件和dll文件呢?第一次接触C++。。。