展开全部
#include "StdAfx.h"
#include "MySqlDB.h"
#include "WinSock2.h"
CMySqlDB::CMySqlDB(void)
: p_mySQL(NULL)
, p_myResult(NULL)
, m_pField(NULL)
, stmt(NULL)
{
Init();
}
CMySqlDB::~CMySqlDB(void)
{
Close();
}
void CMySqlDB::Init(void)
{
p_mySQL=mysql_init(NULL);
}
BOOL CMySqlDB::Open(CString host,CString username,CString password,CString db,DWORD port)
{
if(mysql_real_connect(p_mySQL,host.GetBuffer(),username.GetBuffer(),password.GetBuffer(),db.GetBuffer(),port,NULL,0))
{
return true;
}else
{
return false;
}
}
void CMySqlDB::Close(void)
{
if(stmt!e69da5e6ba903231313335323631343130323136353331333239313564=NULL)
{
mysql_stmt_close(stmt);
stmt=NULL;
}
if(p_myResult!=NULL){
mysql_free_result(p_myResult);
p_myResult=NULL;
}
if(p_mySQL!=NULL)
{
mysql_close(p_mySQL);
p_mySQL=NULL;
}
}
CString CMySqlDB::GetError(void)
{
CString mysqlError=mysql_error(p_mySQL);
CString statmenError=mysql_stmt_error(stmt);
return mysqlError.GetLength()>0?mysqlError:statmenError;
}
BOOL CMySqlDB::Query(CString sql)
{
if(mysql_real_query(p_mySQL,sql.GetBuffer(),sql.GetLength()))
{
return false;
}else
{
p_myResult=mysql_store_result(p_mySQL);
if(p_myResult!=NULL)
{
return true;
}else
{
return false;
}
}
}
int CMySqlDB::GetQueryCount(void)
{
if(p_myResult!=NULL)
{
return (int)mysql_num_fields(p_myResult);
}else
{
return 0;
}
}
int CMySqlDB::GetFieldNum(void)
{
if(p_myResult!=NULL)
{
return mysql_num_fields(p_myResult);
}else
{
return 0;
}
}
BOOL CMySqlDB::MoveNext(void)
{
if(p_myResult!=NULL)
{
p_myRow=mysql_fetch_row(p_myResult);
if(p_myRow)
{
return true;
}else{
return false;
}
}else
{
return false;
}
}
CString CMySqlDB::GetFieldByIndex(int fieldIndex)
{
return CString(m_pField->name);
}
char * CMySqlDB::GetValueByFieldIndex(int fieldIndex)
{
if(fieldIndex>=0&&fieldIndex<=(int)(GetFieldNum()-1))
{
return p_myRow[fieldIndex]?p_myRow[fieldIndex]:"NULL";
}
return "";
}
char * CMySqlDB::GetValueByFieldname(CString fieldName)
{
MYSQL_FIELD *p_Field=NULL;
int fieldIndex=-1;
for(int i=0;i
{
p_Field=mysql_fetch_field_direct(p_myResult,i);
if(CString(p_Field->name)==fieldName)
{
fieldIndex=i;
break;
}
}
if(fieldIndex>=0)
{
return GetValueByFieldIndex(fieldIndex);
}
return "";
}
BOOL CMySqlDB::GetPrepareStatement(CString presql)
{
if(p_mySQL!=NULL)
{
stmt = mysql_stmt_init(p_mySQL);
if(stmt!=NULL)
{
if(mysql_stmt_prepare(stmt,presql,presql.GetLength())==0)
{
int count=0;
for(int i=0;i
{
if(presql.GetAt(i)=='?')
{
count++;
}
}
if(count==(int)mysql_stmt_param_count(stmt))
{
memset(p_bind,0,sizeof(p_bind));
return true;
}
}
}
}
return false;
}
void CMySqlDB::SetPreInt(int index,int data)
{
p_bind[index].buffer_type= MYSQL_TYPE_LONG;
p_bind[index].buffer= (char *)&data;
p_bind[index].is_null=0;
p_bind[index].length= 0;
}
void CMySqlDB::SetPreString(int index, char * data, DWORD len)
{
p_bind[index].buffer_type=MYSQL_TYPE_STRING;
p_bind[index].buffer=data;
p_bind[index].buffer_length=len;
p_bind[index].is_null=0;
}
void CMySqlDB::SetPreBlob(int index, char * data, DWORD len)
{
p_bind[index].buffer_type=MYSQL_TYPE_BLOB;
p_bind[index].buffer=data;
p_bind[index].buffer_length=len;
p_bind[index].is_null=0;
p_bind[index].pack_length=len;
}
BOOL CMySqlDB::ExecutePrepareStatement(void)
{
if(stmt!=NULL)
{
if(mysql_stmt_bind_param(stmt, p_bind)==0)
{
if (mysql_stmt_execute(stmt)==0)
{
if(mysql_stmt_affected_rows(stmt)>0)
{
return true;
}
}
}
}
return false;
}
BOOL CMySqlDB::Execute(CString sql)
{
if(mysql_real_query(p_mySQL,sql.GetBuffer(),sql.GetLength()))
{
return false;
}else
{
return true;
}
}
MySqlDB.cpp
#pragma once
#include "mysql/mysql.h"
#pragma comment(lib,"libmysql.lib")
class CMySqlDB
{
public:
CMySqlDB(void);
~CMySqlDB(void);
private:
MYSQL *p_mySQL;
MYSQL_RES *p_myResult;
MYSQL_ROW p_myRow;
MYSQL_STMT *stmt;
MYSQL_FIELD *m_pField;
MYSQL_BIND p_bind[256];
void Init(void);
void Close(void);
public:
BOOL Open(CString host,CString username,CString password,CString db,DWORD port=MYSQL_PORT);
CString GetError(void);
BOOL Query(CString sql);
int GetQueryCount(void);
int GetFieldNum(void);
BOOL MoveNext(void);
CString GetFieldByIndex(int fieldIndex);
char * GetValueByFieldIndex(int fieldIndex);
char * GetValueByFieldname(CString fieldName);
BOOL GetPrepareStatement(CString presql);
void SetPreInt(int index,int data);
void SetPreString(int index, char * data, DWORD len);
void SetPreBlob(int index, char * data, DWORD len);
BOOL ExecutePrepareStatement(void);
BOOL Execute(CString sql);
};
MySqlDB.h
调用
CMySqlDB db;
CString error;
if(db.Open("127.0.0.1","root","1","mydb"))
{
/*
if(db.Query("select * from info"))
{
int count=db.GetQueryCount();
if(count>0)
{
for(int i=0;i
{
db.MoveNext();
CString data=db.GetValueByFieldname("id");
}
}
}
*/
if(db.GetPrepareStatement("insert into info (name,sex,pic)values(?,?,?)"))
{
//db.SetPreInt(0,0);
db.SetPreString(0,"abc",3);
db.SetPreString(1,"333",3);
CString filePath;
CFileDialog dlg(TRUE);
BYTE *data=NULL;
DWORD len=0;
if(dlg.DoModal()==IDOK)
{
CFile file;
if(file.Open(dlg.GetFileName(),CFile::modeRead))
{
len=file.GetLength();
data=new BYTE[len];
file.Read(data,len);
file.Close();
}
}
db.SetPreBlob(2,(char*)data,len);
if(!db.ExecutePrepareStatement())
{
error=db.GetError();
}
delete data;
}else
{
error=db.GetError();
}
}else
{
MessageBox(db.GetError());
}
已赞过
已踩过<
你对这个回答的评价是?
评论
收起