准备要自己做点小项目了,一直没有用过关系型数据库,这次选用的是Mysql,大致看了一下甲骨文提供的API,能够领会其中的意思,但是感觉总是写API过于繁琐,因此写个类吧,这样能好一点。以下贴出代码是我写的关于mysql操作的类,不足之处,请指正,谢谢哈。
一、mysqlapi.h主要定义了错误代码
-
/*
-
* mysqlapi.h
-
*
-
* Created on: 2013-3-11
-
* Author: er_guangqiang
-
*/
-
-
#ifndef MYSQLAPI_H_
-
#define MYSQLAPI_H_
-
//Errno number
-
#define MO_OK 0x00000000 //操作成功
-
#define HOST_TOO_LONG 0xFFFF0001 //主机名过长
-
#define USERNAME_TOO_LONG 0xFFFF0002 //数据库用户名过长
-
#define PASSWD_TOO_LONG 0xFFFF0003 //数据库密码过长
-
#define DATABASENAME_TOO_LONG 0xFFFF0004 //数据库名字过长
-
#define PORT_SET_ERR 0xFFFF0005 //端口设置错误
-
#define MYSQL_INIT_ERR 0xFFFF0007 //Mysql初始化失败
-
#define MYSQL_CONNECT_ERR 0xFFFF0008 //数据库连接失败
-
#define MYSQL_STOR_RESULTS_ERR 0xFFFF0009 //储存数据库结果失败
-
#define MYSQL_FIELDS_ZERO 0xFFFF000a //获取的字段列为0
-
#define MYSQ_FETCH_FIELDS_ERR 0xFFFF000b //获取字段失败
-
#define HOST_NULL 0xFFFF000c //主机名为空
-
#define USERNAME_NULL 0xFFFF000d //用户名为空
-
#define PASSWD_NULL 0xFFFF000e //数据库密码为空
-
#define MYSQL_SWAP_OUT 0xFFFF000f //mysql_fetch_row结果集已取空
-
#define RESULT_NULL 0xFFFF0010 //结果指针为空,请确认是否执行了获取结果集方法
-
#endif /* MYSQLAPI_H_ */
二、MysqlOperate.h定义了MysqlOperate类的成员变量和成员函数
-
/*
-
* MysqlOperate.h
-
*
-
* Created on: 2013-3-9
-
* Author: er_guangqiang
-
*/
-
-
#ifndef MYSQLOPERATE_H_
-
#define MYSQLOPERATE_H_
-
-
#include <string.h>
-
#include <iostream>
-
#include <mysql.h>
-
#include "mysqlapi.h"
-
-
-
class MysqlOperate {
-
public:
-
MysqlOperate();
-
MysqlOperate(const char *host, const char *user, const char *passwd, const char *db, \
-
unsigned int port, unsigned long clientflag);
-
int InitMysql(const char *host, const char *user, const char *passwd, const char *db, \
-
unsigned int port, unsigned long clientflag);
-
int ConnectMysql();
-
int ExcuteQuery(const char *sqlquery);
-
int FetchResult();
-
unsigned int FetchRowtoBuffer(char **buffer);
-
unsigned long GetRowsCount();
-
unsigned int GetFieldsCount();
-
int FetchColumnName(char **column);
-
void ReleaseConnMysql();
-
virtual ~MysqlOperate();
-
private:
-
MYSQL *m_mysql;
-
MYSQL_RES * m_results;
-
MYSQL_ROW m_record;
-
MYSQL_FIELD *m_fields;
-
char m_host[16];
-
char m_user[16];
-
char m_passwd[32];
-
char m_db[16];
-
int m_port;
-
long m_clientflag;
-
};
-
-
#endif /* MYSQLOPERATE_H_ */
三、MysqlOperate.cpp主要实现头文件中定义的成员函数
-
/*
-
* MysqlOperate.cpp
-
*
-
* Created on: 2013-3-9
-
* Author: er_guangqiang
-
*/
-
-
#include "MysqlOperate.h"
-
-
//构造函数,主要任务复位成员变量
-
MysqlOperate::MysqlOperate() {
-
// TODO Auto-generated constructor stub
-
memset(m_host, 0x00, 16);
-
memset(m_user, 0x00, 16);
-
memset(m_passwd, 0x00, 32);
-
memset(m_db, 0x00, 16);
-
m_port = 3306;
-
m_clientflag = 0;
-
m_mysql = NULL;
-
m_results = NULL;
-
}
-
-
/*重载构造函数
-
*输入: host:主机ip
-
* user:mysql用户名
-
* passwd:mysql密码
-
* db:选择mysql中的数据库
-
* port:设置mysql端口
-
* clientflag:mysql客户端参数
-
*输出:
-
* 无
-
* 次函数主要负责初始化Mysql的信息
-
*/
-
MysqlOperate::MysqlOperate(const char *host, const char *user, const char *passwd, const char *db, \
-
unsigned int port, unsigned long clientflag)
-
{
-
int r = 0;
-
memset(m_host, 0x00, 16);
-
memset(m_user, 0x00, 16);
-
memset(m_passwd, 0x00, 32);
-
memset(m_db, 0x00, 16);
-
m_port = 3306;
-
m_clientflag = 0;
-
r = InitMysql(host,user,passwd,db,port,clientflag);
-
if(0 == r)
-
std::cout<< "InitMysql Success!!!"<<std::endl;
-
else
-
std::cout << "Errno = " << r << std::endl;
-
-
m_mysql = NULL;
-
m_results = NULL;
-
}
-
//初始化mysql信息
-
/*
-
* 输入:与MysqlOperate()构造函数相同
-
*/
-
int MysqlOperate::InitMysql(const char *host, const char *user, const char *passwd, const char *db, \
-
unsigned int port, unsigned long clientflag)
-
{
-
if(NULL == host)
-
return HOST_NULL;
-
else if(strlen(host) > 16)
-
return HOST_TOO_LONG;
-
else if (strlen(host) >= 0 && strlen(host) <= 16)
-
strncpy(m_host, host, strlen(host));
-
-
if(NULL == user)
-
return USERNAME_NULL;
-
else if(strlen(user) > 16)
-
return USERNAME_TOO_LONG;
-
else if(strlen(user) >= 0 && strlen(user) <= 16)
-
strncpy(m_user, user, strlen(user));
-
-
if(NULL == passwd)
-
return PASSWD_NULL;
-
else if(strlen(passwd) > 32)
-
return PASSWD_TOO_LONG;
-
else if(strlen(passwd) >= 0 && strlen(passwd) <= 32)
-
strncpy(m_passwd, passwd, strlen(passwd));
-
-
if(NULL == db)
-
{
-
std::cout << "Warning!!!---database set null---! Will you create database??"<< std::endl;
-
return MO_OK;
-
}
-
else if(strlen(db) > 16)
-
return DATABASENAME_TOO_LONG;
-
else
-
strncpy(m_db, db, strlen(db));
-
if(port <= 0)
-
return PORT_SET_ERR;
-
else
-
m_port = port;
-
m_clientflag = clientflag;
-
return MO_OK;
-
}
-
-
//连接Mysql数据库
-
/*
-
* 输入:
-
* 无
-
* 输出:
-
* 无
-
* 此函数旨在连接Mysql数据库
-
*/
-
int MysqlOperate::ConnectMysql()
-
{
-
m_mysql = mysql_init(NULL);
-
if(NULL == m_mysql)
-
{
-
std::cout << "mysql_init failed" << std::endl;
-
return MYSQL_INIT_ERR;
-
}
-
else
-
std::cout << "mysql_init success" << std::endl;
-
mysql_real_connect(m_mysql, m_host, m_user, m_passwd, m_db, m_port, NULL, m_clientflag);
-
if(NULL == m_mysql)
-
{
-
std::cout << "mysql_real_connect failed" << std::endl;
-
return MYSQL_CONNECT_ERR;
-
}
-
else
-
std::cout << "mysql_real_connect success" << std::endl;
-
return MO_OK;
-
}
-
-
//执行sql语句
-
/*
-
* 输入:
-
* sqlquery:sql语句字符串
-
* 输出:
-
* 无
-
* 此函数为执行定义的sql语句
-
*/
-
int MysqlOperate::ExcuteQuery(const char *sqlquery)
-
{
-
return mysql_query(m_mysql, sqlquery);
-
}
-
-
int MysqlOperate::FetchResult()
-
{
-
m_results = mysql_store_result(m_mysql);
-
if(NULL == m_results)
-
{
-
std::cout << "mysql_store_result failed" << std::endl;
-
return MYSQL_STOR_RESULTS_ERR;
-
}
-
else
-
std::cout << "mysql_store_result success" << std::endl;
-
return MO_OK;
-
}
-
-
//获取行内容(一行)
-
/*
-
* 输入/输出:
-
* buffer:行内容存储容器
-
* 此函数为获取结果集的一行内容(在此之前一定要先执行sql语句,保证m_results不为空)
-
*/
-
unsigned int MysqlOperate::FetchRowtoBuffer(char **buffer)
-
{
-
if(NULL == m_results)
-
return RESULT_NULL;
-
if((m_record = mysql_fetch_row(m_results)))
-
{
-
int c = GetFieldsCount();
-
int i = 0;
-
while(i < c)
-
{
-
strncpy(*(buffer + i), m_record[i], strlen(m_record[i]));
-
i++;
-
}
-
return MO_OK;
-
}
-
return MYSQL_SWAP_OUT;
-
-
}
-
-
//获取字段个数
-
/*
-
* 输入/输出:
-
* 无
-
* 此函数为获取结果集的字段个数
-
*/
-
unsigned int MysqlOperate::GetFieldsCount()
-
{
-
if(NULL == m_results)
-
{
-
std::cout << "m_results is NULL, please confirm mysql_store_result success" << std::endl;
-
return MO_OK;
-
}
-
return mysql_num_fields(m_results);
-
}
-
-
//获取行数
-
/*
-
* 输入/输出:
-
* 无
-
* 此函数为获取结果集的行数
-
*/
-
unsigned long MysqlOperate::GetRowsCount()
-
{
-
if(NULL == m_results)
-
{
-
std::cout << "m_results is NULL, please confirm mysql_store_result success" << std::endl;
-
return MO_OK;
-
}
-
return (unsigned long)mysql_num_rows(m_results);
-
}
-
-
//获取字段内容
-
/*
-
* 输入/输出:
-
* column:字段内容容器
-
* 此函数为获取结果集的字段内容
-
*/
-
int MysqlOperate::FetchColumnName(char **column)
-
{
-
unsigned int c = GetFieldsCount();
-
if(0 == c)
-
{
-
std::cout << "GetFieldsCount is zero." << std::endl;
-
return MYSQL_FIELDS_ZERO;
-
}
-
m_fields = mysql_fetch_field(m_results);
-
if(NULL == m_fields)
-
{
-
std::cout << "mysql_fetch_field is failed" << std::endl;
-
return MYSQ_FETCH_FIELDS_ERR;
-
}
-
else
-
std::cout << "mysql_fetch_field is success" <<std::endl;
-
unsigned int i = 0;
-
for(; i < c; i++)
-
strncpy(column[i], m_fields[i].name, m_fields[i].name_length);
-
return MO_OK;
-
}
-
-
//释放连接
-
/*
-
* 输入/输出:
-
* 无
-
* 此函数为释放Mysql连接
-
*/
-
void MysqlOperate::ReleaseConnMysql()
-
{
-
if(NULL != m_results)
-
mysql_free_result(m_results);
-
if(NULL != m_mysql)
-
mysql_close(m_mysql);
-
}
-
-
MysqlOperate::~MysqlOperate() {
-
// TODO Auto-generated destructor stub
-
}
以上就是我的代码,里面有函数的输入输出了功能简介,再次不赘述,我是新人,指教哈
阅读(2386) | 评论(0) | 转发(1) |