加入收藏 | 设为首页 | 会员中心 | 我要投稿 我爱资讯网 (https://www.52junxun.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 服务器 > 搭建环境 > Unix > 正文

Linux(Unix)下MySQL数据库访问接口程序MCI (MySQL Cal

发布时间:2022-11-10 12:50:35 所属栏目:Unix 来源:
导读:  需要的朋友可加我QQ,我把源代码全部发给你!

  mci.h

  #ifndef _MCI_H_

  #define _MCI_H_

  #include

  #include

  #include

  #include

  #include

  
  需要的朋友可加我QQ,我把源代码全部发给你!
 
  mci.h
 
  #ifndef _MCI_H_
 
  #define _MCI_H_
 
  #include
 
  #include
 
  #include
 
  #include
 
  #include
 
  #include
 
  #include "mysql.h"
 
  const unsigned int MAX_FIELD_LEN = 1024*1;
 
  class MCIException
 
  {
 
  public:
 
  int ErrNo;
 
  charErrInfo[256];
 
  public:
 
  MCIException(const char *errinfo,int errno);
 
  char *getErrInfo();
 
  int getErrNo(){return ErrNo;};
 
  //自定义错误类型
 
  //1 不支持的字段类型
 
  //2 字段越界
 
  //3 字段不存在
 
  //MySQL内部错误类型
 
  //2002 Can't connect to local MySQL server through socket
 
  //2003 Can't connect to MySQL server
 
  //2006 MySQL server has gone away
 
  //2013 Lost connection to MySQL server during query
 
  //1045 Access denied for user
 
  };
 
  class MCIDatabase
 
  {
 
  public:
 
  int Valid;
 
  char DBIP[20]; //数据库IP地址
 
  char User[10]; //用户名
 
  char Pwd[10]; //密码
 
  char DBName[20]; //数据库名
 
  MYSQL *mysql;
 
  public:
 
  MCIDatabase();
 
  ~MCIDatabase(){};
 
  MYSQL* getMySQL(){return mysql;};
 
  void setLogin(const char* dbip,const char* usr, const char* pwd, const char* dbname) ;
 
  int connect();
 
  void disConnect();
 
  };
 
  class MCIField
 
  {
 
  public:
 
  friend class MCIQuery;
 
  MCIQuery* pParentQuery;//指向该Field所属于的Query
 
  char FieldName[30]; //字段名称(目前支持30长度)
 
  char StrBuf[255];//用于保存转换为字符串后的值
 
  unsigned char* DataBuf;//预绑定缓冲区
 
  enum_field_types FieldType;//MySQL内部数据类型
 
  unsigned int FieldLength;//数据长度
 
  public:
 
  MCIField();
 
  ~MCIField();
 
  void setFieldName(const char* s);
 
  void setFieldType(enum_field_types n);
 
  void setFieldLength(unsigned int n);
 
  char* getFieldName();
 
  char* getStrBuf();
 
  enum_field_types getFieldType();
 
  unsigned int getFieldLength();
 
  MCIQuery* getParentQuery();
 
  void setParentQuery(MCIQuery* pQry);
 
  static void trimLeft(char* str);
 
  static void trimRight(char* str);
 
  static char* allTrim(char* str);
 
  char* asString();
 
  int asInteger();
 
  float asFloat();
 
  char asChar(int pos = 0);
 
  };
 
  class MCIQuery
 
  {
 
  public:
 
  MCIDatabase* pDB;
 
  MYSQL_RES* pRes;
 
  int FieldNum; //字段个数
 
  MYSQL_FIELD* pFields; //得到的字段信息
 
  MCIField* pMCIFieldList; //在内部保存的所有字段信息
 
  MYSQL_ROW Row;
 
  int RowNum;
 
  char SqlStr[1024*3];
 
  int CurrRow;
 
  public:
 
  MCIQuery();
 
  void setDB(MCIDatabase *dblink);
 
  ~MCIQuery();
 
  void setSql(char* sqlstr);
 
  void open(); //执行select型SQL语句
 
  int getFieldsDef(); //获得字段信息,并为字段分配取值的缓冲区
 
  int getRecordCount();//返回查询到的符合条件的记录的条数
 
  int next(); //移动到下一个记录,同时获取字段值
 
  MCIField* field(int i); //取相应字段值
 
  MCIField* fieldByName(const char* s);
 
  void exec(); //执行insert,update型SQL语句,返回被此语句影响的记录条数
 
  void close();//关闭一个Query,为下次执行做准备
 
  };
 
  #endif
 
  makefile
 
  CC = g++
 
  CFLAGS = -Wall
 
  SQLHOME = -L/usr/lib64 -lmysqlclient
 
  all:test
 
  test: mci.o test.o
 
  $(CC) -s -o test -m64 *.o $(SQLHOME)
 
  mci.o: mci.cpp mci.h
 
  $(CC) $(CFLAGS) -c mci.cpp
 
  test.o: test.cpp test.h
 
  $(CC) $(CFLAGS) -c test.cpp
 
  clean::
 
  rm -f *.o
 

(编辑:我爱资讯网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章