/****************************************************
Copyright (C), 2010-2014, Jovision Tech. Co., Ltd.
File name : mysqlhelper.h
Author : loong
Version : 1.0
Date : 2014-03-07
Description : 数据库存储业务模块
Function List:
1.dba_connect : 连接数据库
2.dba_disconnect : 断开数据库连接
3.dba_executeprocedure : 执行存储过程
4.dba_queryresult : 获取查询结果集
5.dba_nextrow : 结果集的下一行
6.dba_seekrow : 转到结果集某一行
7.dba_getfieldcount : 获取结果集的列数
8.dba_getfieldname : 获取列名称
9.dba_getfieldvalue : 获取列值
10.dba_getfieldvaluebyname : 根据列名获取列值
11.dba_findfieldname : 查找列名称
12.dba_freeresult : 释放结果集
14.dba_geterrormsg : 获取错误信息
****************************************************/
#ifndef VC_EXTRALEAN
#define VC_EXTRALEAN // 从 Windows 头中排除极少使用的资料
#endif
#include <Windows.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include "mysqlhelper.h"
/*连接数据库*/
int dba_connect(DBA *dba, const char *host, const char *user, const char * passwd, const char * db, unsigned int port)
{
if(dba == NULL)
return MYSQL_ERROR;
if(host == NULL || user == NULL || passwd == NULL || db == NULL || port < 1)
{
return MYSQL_ERROR;
}
#ifdef _WINDOWS
_snprintf_s(dba->db_host, sizeof(dba->db_host) - 1, sizeof(dba->db_host) - 1, "%s", host);
_snprintf_s(dba->db_user, sizeof(dba->db_user) - 1, sizeof(dba->db_user) - 1, "%s", user);
_snprintf_s(dba->db_passwd, sizeof(dba->db_passwd) - 1, sizeof(dba->db_passwd) - 1, "%s", passwd);
_snprintf_s(dba->db_name, sizeof(dba->db_name) -1, sizeof(dba->db_name) -1, "%s", db);
#else
_snprintf(dba->db_host, sizeof(dba->db_host) - 1, "%s", host);
_snprintf(dba->db_user, sizeof(dba->db_user) - 1, "%s", user);
_snprintf(dba->db_passwd, sizeof(dba->db_passwd) - 1, "%s", passwd);
_snprintf(dba->db_name, sizeof(dba->db_name) -1, "%s", db);
#endif
dba->db_port = (port < 1 ? 3306 : port);
//mysql_library_init(0, NULL, NULL);
mysql_init(&dba->mysql);
if(mysql_real_connect(&dba->mysql, dba->db_host, dba->db_user, dba->db_passwd, dba->db_name, dba->db_port, NULL, 0) == NULL)
{
#ifdef _WINDOWS
_snprintf_s(dba->errmsg, sizeof(dba->errmsg) - 1, sizeof(dba->errmsg) - 1, "Unable to connect to the database,The cause of the error is :%s.", mysql_error(&dba->mysql));
#else
_snprintf(dba->errmsg, sizeof(dba->errmsg) - 1, "Unable to connect to the database,The cause of the error is :%s.", mysql_error(&dba->mysql));
#endif
mysql_library_end();
return MYSQL_ERROR;
}
dba->errmsg[0] = 0;
return 0;
}
/*断开数据库连接*/
void dba_disconnect(DBA *dba)
{
if(dba == NULL)
return;
if(dba->res != NULL)
{
dba_freeresult(dba);
}
mysql_close(&dba->mysql);
//mysql_library_end();
dba->errmsg[0] = 0;
}
/*执行存储过程*/
int dba_execute_procedure(DBA *dba, const char *proc, const char *paramters, ...)
{
int nlen;
unsigned long ret;
va_list ap;
char sql[MAX_SQLBUF_LEN] = {0};
if(dba == NULL)
return 0;
if(dba->res != NULL)
{
dba_freeresult(dba);
}
#ifdef _WINDOWS
_snprintf_s(sql, sizeof(sql) - 1, sizeof(sql) - 1, "call %s(", proc);
#else
_snprintf(sql, sizeof(sql) - 1, "call %s(", proc);
#endif
nlen = strlen(sql);
va_start(ap, paramters);
#ifdef _WINDOWS
vsnprintf_s(sql + nlen, MAX_SQLBUF_LEN - nlen - 1, MAX_SQLBUF_LEN - nlen - 1, paramters, ap);
#else
vsnprintf(sql + nlen, MAX_SQLBUF_LEN - nlen - 1, paramters, ap);
#endif
va_end(ap);
nlen = strlen(sql);
#ifdef _WINDOWS
_snprintf_s(sql + nlen, sizeof(sql) - 1, sizeof(sql) - 1, ");");
#else
_snprintf(sql + nlen, sizeof(sql) - 1, ");");
#endif
//printf("sql:%s\n", sql);
mysql_query(&dba->mysql, "SET NAMES utf8");
if(mysql_real_query(&dba->mysql, sql, strlen(sql)) != 0)
{
#ifdef _WINDOWS
_snprintf_s(dba->errmsg, sizeof(dba->errmsg) - 1, sizeof(dba->errmsg) - 1, "Execute the %s procdure failure, The cause of the error is:%s.", proc, mysql_error(&dba->mysql));
#else
_snprintf(dba->errmsg, sizeof(dba->errmsg) - 1, "Execute the %s procdure failure, The cause of the error is:%s.", proc, mysql_error(&dba->mysql));
#endif
return MYSQL_ERROR;
}
dba->errmsg[0] = 0;
ret = (unsigned long)mysql_affected_rows(&dba->mysql);
return ret;
}
/* 执行语句 */
int dba_execute(DBA *dba, const char *paramters, ...)
{
int ret;
va_list ap;
char sql[MAX_SQLBUF_LEN] = {0};
if(dba == NULL)
return 0;
if(dba->res != NULL)
{
dba_freeresult(dba);
}
va_start(ap, paramters);
#ifdef _WINDOWS
vsnprintf_s(sql, MAX_SQLBUF_LEN - 1, MAX_SQLBUF_LEN - 1, paramters, ap);
#else
vsnprintf(sql, MAX_SQLBUF_LEN - 1, paramters, ap);
#endif
va_end(ap);
//printf("sql:%s\n", sql);
mysql_query(&dba->mysql, "SET NAMES utf8");
if(mysql_real_query(&dba->mysql, sql, strlen(sql)) != 0)
{
return MYSQL_ERROR;
}
dba->errmsg[0] = 0;
ret = (unsigned long)mysql_affected_rows(&dba->mysql);
if(ret < 0)
{
ret = 0;
}
return ret;
}
/*获取查询结果集*/
int dba_queryresult(DBA *dba)
{
if(dba == NULL)
return 0;
dba->res = mysql_store_result(&dba->mysql);
if(dba->res == NULL)
{
#ifdef _WINDOWS
_snprintf_s(dba->errmsg, sizeof(dba->errmsg) - 1, sizeof(dba->errmsg) - 1, "%s", "The query result set is empty.");
#else
_snprintf(dba->errmsg, sizeof(dba->errmsg) - 1, "%s", "The query result set is empty.");
#endif
dba->nfieldcount = 0;
}
else
{
dba->errmsg[0] = 0;
dba->nrowcount = (int)dba->res->row_count;
if(dba->nrowcount > 0)
{
dba->row = mysql_fetch_row(dba->res);
}
dba->nfieldcount = mysql_num_fields(dba->res);
}
return (dba->res == NULL ? 0 : dba->nrowcount);
}
/*结果集的下一行*/
int dba_nextrow(DBA *dba)
{
if(dba == NULL)
return MYSQL_ERROR;
if(dba->res == NULL)
{
#ifdef _WINDOWS
_snprintf_s(dba->errmsg, sizeof(dba->errmsg) - 1, sizeof(dba->errmsg) - 1, "%s", "The query result set is empty.");
#else
_snprintf(dba->errmsg, sizeof(dba->errmsg) - 1, "%s", "The query result set is empty.");
#endif
return MYSQL_ERROR;
}
if(!mysql_eof(dba->res))
{
#ifdef _WINDOWS
_snprintf_s(dba->errmsg, sizeof(dba->errmsg) - 1, sizeof(dba->errmsg) - 1, "%s", "The current row is the last row.");
#else
_snprintf(dba->errmsg, sizeof(dba->errmsg) - 1, "%s", "The current row is the last row.");
#endif
return MYSQL_ERROR;
}
dba->row = mysql_fetch_row(dba->res);
if(dba->row == NULL)
{
#ifdef _WINDOWS
_snprintf_s(dba->errmsg, sizeof(dba->errmsg) - 1, sizeof(dba->errmsg) - 1, "%s", "Failed to move to the next row.");
#else
_snprintf(dba->errmsg, sizeof(dba->errmsg) - 1, "%s", "Failed to move to the next row.");
#endif
return MYSQL_ERROR;
}
dba->errmsg[0] = 0;
return 0;
}
/*转到结果集某一行*/
int dba_seekrow(DBA *dba, int nrowindex)
{
if(dba == NULL)
return MYSQL_ERROR;
dba->errmsg[0] = 0;
if(dba->res != NULL && nrowindex >-1 && nrowindex < dba->nrowcount)
{
mysql_data_seek(dba->res, nrowindex);
return 0;
}
return MYSQL_ERROR;
}
/*获取结果集的列数*/
int dba_getfieldcount(DBA *dba)
{
if(dba == NULL)
return 0;
dba->errmsg[0] = 0;
return dba->nfieldcount;
}
/*获取列名称*/
char* dba_getfieldname(DBA *dba, int nindex)
{
if(dba == NULL)
return NULL;
dba->errmsg[0] = 0;
if(dba->res != NULL && nindex > -1 && nindex < dba->nfieldcount)
{
MYSQL_FIELD *field = mysql_fetch_field_direct(dba->res, nindex);
if(field == NULL)
{
return NULL;
}
return field->name;
}
return NULL;
}
/*获取列值*/
char* dba_getfieldvalue(DBA *dba, int nindex)
{
if(dba == NULL)