#include <stdio.h>
#include <string.h>
#include <memory.h>
//#include <WINSOCK2.H>
//#include <mysql.h>
# include <mysql/mysql.h>
# include <stdlib.h>
#include "types.h"
#include "cmysql.h"
CMySQL::CMySQL(const char *host, unsigned int port, const char *user, const char *passwd, const char *db/* =NULL */)
{
mysql_init(&m_mysql);
m_bConnected = FALSE;
m_pSQL=NULL;
m_num_rows=0;
m_num_fields=0;
m_bError=FALSE;
m_result=NULL;
SetHost(host);
SetPort(port);
SetUser(user);
SetPassWord(passwd);
SetDB(db);
}
BOOL CMySQL::Connect()
{
if (!mysql_real_connect(&m_mysql,m_szhost,m_szUser,m_szPasswd,m_szDB,m_iPort,NULL,0))
{
if (mysql_errno(&m_mysql))
{
strcpy(m_szError,(char*)mysql_error(&m_mysql));
m_bError=TRUE;
}
m_bConnected = FALSE;
}
else
{
m_bConnected = TRUE;
m_bError=FALSE;
}
return m_bConnected;
}
CMySQL::CMySQL()
{
mysql_init(&m_mysql);
m_bConnected = FALSE;
m_pSQL=NULL;
m_num_rows=0;
m_num_fields=0;
m_bError=FALSE;
m_result=NULL;
}
CMySQL::~CMySQL()
{
DisConnect();
}
void CMySQL::DisConnect()
{
Close();
mysql_close(&m_mysql);
m_bConnected = FALSE;
m_bError=FALSE;
if (m_pSQL!=NULL)
{
delete [] m_pSQL;
}
}
BOOL CMySQL::Connect(const char *host, unsigned int port, const char *user, const char *passwd, const char *db/* =NULL */)
{
SetHost(host);
SetPort(port);
SetUser(user);
SetPassWord(passwd);
SetDB(db);
return Connect();
}
BOOL CMySQL::UseDB(const char *db)
{
if (!mysql_select_db(&m_mysql,db))
{
if (mysql_errno(&m_mysql))
{
strcpy(m_szError,(char*)mysql_error(&m_mysql));
m_bError=TRUE;
}
return FALSE;
}
return TRUE;
}
void CMySQL::StartTransaction()
{
mysql_autocommit(&m_mysql,FALSE);
}
void CMySQL::Commit()
{
mysql_commit(&m_mysql);
mysql_autocommit(&m_mysql,TRUE);
}
void CMySQL::RollBack()
{
mysql_rollback(&m_mysql);
mysql_autocommit(&m_mysql,TRUE);
}
void CMySQL::SetSQL(const char *sql)
{
if (m_pSQL!=NULL)
{
delete [] m_pSQL;
m_pSQL=NULL;
}
m_pSQL=new char[strlen(sql)+1];
memcpy(m_pSQL,sql,strlen(sql)+1);
}
char* CMySQL::GetSQL()
{
return m_pSQL;
}
BOOL CMySQL::ExcuteSQL(const char* sql/* =NULL */)
{
Close();
if (sql!=NULL)
{
SetSQL(sql);
}
if (mysql_query(&m_mysql,m_pSQL))
{
if (mysql_errno(&m_mysql))
{
strcpy(m_szError,(char*)mysql_error(&m_mysql));
m_bError=TRUE;
}
return FALSE;
}
return TRUE;
}
BOOL CMySQL::Open(enum ResultMode rm)
{
if (m_result!=NULL)
{
mysql_free_result(m_result);
m_num_rows=0;
m_num_fields=0;
m_result=NULL;
}
if (rm==rmStoreResult)
{
m_result = mysql_store_result(&m_mysql);
}
else
{
m_result = mysql_use_result(&m_mysql);
}
if (m_result==NULL)
{
if (mysql_errno(&m_mysql))
{
strcpy(m_szError,(char*)mysql_error(&m_mysql));
m_bError=TRUE;
}
return FALSE;
}
m_num_rows=mysql_num_rows(m_result);
m_num_fields = mysql_num_fields(m_result);
m_fields = mysql_fetch_fields(m_result);
//Next();
return TRUE;
}
void CMySQL::Close()
{
if (m_result!=NULL)
{
mysql_free_result(m_result);
m_num_rows=0;
m_num_fields=0;
m_result=NULL;
}
}
/*是否到记录尾*/
BOOL CMySQL::IsEOF()
{
if(!mysql_eof(m_result))
{
if (mysql_errno(&m_mysql))
{
strcpy(m_szError,(char*)mysql_error(&m_mysql));
m_bError=TRUE;
}
return FALSE;
}
return TRUE;
}
/*是否是记录头*/
//BOOL CMySQL::IsBOF()
//{
//
//}
/*第一条*/
//void CMySQL::First()
//{
//
//}
/*上一条*/
//BOOL CMySQL::Prior()
//{
//
//}
/*下一条*/
BOOL CMySQL::Next()
{
if ((m_row=mysql_fetch_row(m_result)))
{
m_lengths=mysql_fetch_lengths(m_result);
return TRUE;
}
else
{
return FALSE;
}
}
BOOL CMySQL::GetFieldByName(const char* FieldName, char* Buf, int bufsize)
{
if (m_num_fields<=0)
{
return FALSE;
}
char lszFieldName1[255],lszFieldName2[255];
strcpy(lszFieldName1,FieldName);
MakeUpper(lszFieldName1);
for (int i=0;i<(int) m_num_fields;i++)
{
strcpy(lszFieldName2,m_fields[i].name);
MakeUpper(lszFieldName2);
if (strcmp(lszFieldName1,lszFieldName2)==0)
{
if ((int) m_lengths[i]<bufsize)
{
sprintf(Buf,"%.*s",(int) m_lengths[i], m_row[i]);
if (m_fields[i].type==MYSQL_TYPE_DOUBLE ||
m_fields[i].type==MYSQL_TYPE_FLOAT ||
m_fields[i].type==MYSQL_TYPE_LONG ||
m_fields[i].type==MYSQL_TYPE_SHORT ||
m_fields[i].type==MYSQL_TYPE_TINY ||
m_fields[i].type==MYSQL_TYPE_DECIMAL ||
m_fields[i].type==MYSQL_TYPE_LONGLONG ||
m_fields[i].type==MYSQL_TYPE_INT24)
{
if (m_lengths[i]==0)
{
sprintf(Buf,"");
}
}
return TRUE;
}
}
}
return FALSE;
}
BOOL CMySQL::GetFieldValue(const unsigned int Index, char* Buf, unsigned long bufsize)
{
if (Index<0 || Index>=m_num_fields || bufsize<m_lengths[Index])
{
return FALSE;
}
sprintf(Buf,"%.*s",(int) m_lengths[Index], m_row[Index]);
if (m_fields[Index].type==MYSQL_TYPE_DOUBLE ||
m_fields[Index].type==MYSQL_TYPE_FLOAT ||
m_fields[Index].type==MYSQL_TYPE_LONG ||
m_fields[Index].type==MYSQL_TYPE_SHORT ||
m_fields[Index].type==MYSQL_TYPE_TINY ||
m_fields[Index].type==MYSQL_TYPE_DECIMAL ||
m_fields[Index].type==MYSQL_TYPE_LONGLONG ||
m_fields[Index].type==MYSQL_TYPE_INT24)
{
if (m_lengths[Index]==0)
{
sprintf(Buf,"0");
}
}
return TRUE;
}
my_ulonglong CMySQL::GetRecordCount()
{
return m_num_rows;
}
void MakeUpper(char* str) //转换为大写
{
for (unsigned int i=0;i<strlen(str);i++)
{
if (str[i]>='a' && str[i]<='z')
{
str[i]-=0x20;
}
}
}
评论0