Chinaunix首页 | 论坛 | 博客
  • 博客访问: 146759
  • 博文数量: 32
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 515
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-11 09:57
文章分类

全部博文(32)

文章存档

2011年(1)

2009年(4)

2008年(27)

我的朋友

分类: 数据库开发技术

2008-06-09 14:45:57

sqlite_db.cpp
/****************************************************************/
 //---------------------------------------------------------------------------
#pragma hdrstop
#include "sqlite_db.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
sqlite_data::sqlite_data()
{
    m_data = NULL;
    m_RowCount = 0;
    m_ColCount = 0;
}
//---------------------------------------------------------------------------
//拷贝构造不能复制,可以用Clone方法复制
sqlite_data::sqlite_data(const sqlite_data &X)
{
    m_data = NULL;
    m_RowCount = 0;
    m_ColCount = 0;
}
//---------------------------------------------------------------------------
sqlite_data::~sqlite_data()
{
    Free();
}
//---------------------------------------------------------------------------
//复制一个结果集,待完善
void sqlite_data::Clone(sqlite_data &X)
{
    m_RowCount = X.m_RowCount;
    m_ColCount = X.m_ColCount;
//    m_data
}
//---------------------------------------------------------------------------
//重载(),取得结果
const char* sqlite_data::operator()(int Row, int Col)
{
    return GetData(Row, Col);
}
//---------------------------------------------------------------------------
//取得某行某列结果,不存在则返回NULL
const char* sqlite_data::GetData(int Row, int Col)
{
    if(Row > m_RowCount || Col > m_ColCount || Col == 0)
        return NULL;
    else
        return m_data[m_ColCount*Row+Col-1];
}
//---------------------------------------------------------------------------
//释放结果集
void sqlite_data::Free()
{
    if(NULL != m_data)
    {
        sqlite3_free_table(m_data);
        m_data = NULL;
        m_RowCount = 0;
        m_ColCount = 0;
    }
}
//---------------------------------------------------------------------------
//返回行数
int sqlite_data::RowCount()
{
    return m_RowCount;
}
//---------------------------------------------------------------------------
//返回列数
int sqlite_data::ColCount()
{
    return m_ColCount;
}
//---------------------------------------------------------------------------
//结果集是否打开
bool sqlite_data::Active()
{
    return (m_data == NULL);
}
//---------------------------------------------------------------------------
//结果集是否为空
bool sqlite_data::IsEmpty()
{
    return (m_RowCount == 0);
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
sqlite_db::sqlite_db()
{
    m_db = NULL;
    memset(m_dbName, 0, MAX_DBNAME);
}
//---------------------------------------------------------------------------
sqlite_db::sqlite_db(const char* dbName)
{
    Open(dbName);
}
//---------------------------------------------------------------------------
sqlite_db::~sqlite_db()
{
    Close();
}
//---------------------------------------------------------------------------
//返回SQLite的版本
const char* sqlite_db::Version()
{
    return SQLITE_VERSION;
}
//---------------------------------------------------------------------------
//返回打开的数据库名
const char* sqlite_db::DataBaseName()
{
    return m_dbName;
}
//---------------------------------------------------------------------------
//打开数据库
bool sqlite_db::Open(const char* dbName)
{
    if(Connected())
    {
        Close();
    }
    if(NULL == dbName)
    {
        return false;
    }
    if(strlen(dbName) >= MAX_DBNAME)
    {
        return false;
    }
    strcpy(m_dbName, dbName);
    int r = sqlite3_open(m_dbName,&m_db);
    if(SQLITE_OK == r)
    {
        return true;
    }
    else
    {
        Close();
        return false;
    }
}
//---------------------------------------------------------------------------
//关闭数据库
void sqlite_db::Close()
{
    Free();
    if(NULL != m_db)
    {
        sqlite3_close(m_db);
        m_db = NULL;
        memset(m_dbName, 0, MAX_DBNAME);
    }
}
//---------------------------------------------------------------------------
//是否打开数据库状态
bool sqlite_db::Connected()
{
    return (NULL != m_db);
}
//---------------------------------------------------------------------------
//执行查询
int sqlite_db::Query(const char* SQL)
{
    return  Query(Data, SQL);
}
//---------------------------------------------------------------------------
//执行查询
int sqlite_db::Query(sqlite_data &ResultData, const char* SQL)
{
    if(!Connected())
    {
        return -1;
    }
    if(NULL == SQL)
    {
        return -1;
    }
    ResultData.Free();
    char *pcErrMsg;          /* 返回错误信息*/
    int r = sqlite3_get_table(m_db, SQL, &ResultData.m_data, &ResultData.m_RowCount, &ResultData.m_ColCount, &pcErrMsg);
    if(SQLITE_OK == r)
    {
        return ResultData.RowCount();
    }
    else
    {
        sqlite3_free(pcErrMsg);
        ResultData.Free();
        return  -1;
    }
}
//---------------------------------------------------------------------------
//执行SQL命令
int sqlite_db::ExecSQL(const char* SQL)
{
    if(!Connected())
    {
        return -1;
    }
    if(NULL == SQL)
    {
        return -1;
    }
    char *pcErrMsg = NULL;
    int Row = 0;
    int r = sqlite3_exec(m_db, SQL, sqlite_callback, (void*)&Row, &pcErrMsg);
    if(SQLITE_OK == r)
    {
        return Row;
    }
    else
    {
        sqlite3_free(pcErrMsg);
        return -1;
    }
}
//---------------------------------------------------------------------------
//回调函数
int sqlite_callback(void *param,int colcount,char **cols,char **colnames)
{
  return 0;
}
//---------------------------------------------------------------------------
void sqlite_db::Free()
{
    Data.Free();
}
//---------------------------------------------------------------------------
bool sqlite_db::IsEmpty()
{
    return Data.IsEmpty();
}
//---------------------------------------------------------------------------
bool sqlite_db::Active()
{
    return Data.Active();
}
//---------------------------------------------------------------------------
int sqlite_db::RowCount()
{
    return Data.RowCount();
}
//---------------------------------------------------------------------------
int sqlite_db::ColCount()
{
    return Data.ColCount();
}
//---------------------------------------------------------------------------
/****************************************************************/
sqlite_db.h文件
/****************************************************************/
//---------------------------------------------------------------------------
#ifndef sqlite_dbH
#define sqlite_dbH
//---------------------------------------------------------------------------
#include "sqlite3.h"
#include
//---------------------------------------------------------------------------
#define MAX_DBNAME      255
//---------------------------------------------------------------------------
class  sqlite_db;
struct sqlite_data
{
  public:
      sqlite_data();
      sqlite_data(const sqlite_data &X);
      ~sqlite_data();
      void Clone(sqlite_data &X);
      const char* operator()(int Row, int Col);
      const char* GetData(int Row, int Col);
      void Free();
  public:
   int RowCount();
   int ColCount();
   bool Active();
   bool IsEmpty();
  private:
      friend sqlite_db;
      char** m_data;
     
      int m_RowCount;
      int m_ColCount;
};
//---------------------------------------------------------------------------
class  sqlite_db
{
  public:
      sqlite_db();
      sqlite_db(const char* dbName);
      ~sqlite_db();
      bool Open(const char* dbName);
      void Close();
      int ExecSQL(const char* SQL);
      int Query(sqlite_data &ResultData, const char* SQL);
      int Query(const char* SQL);
  public:
      bool Connected();
   const char* DataBaseName();
      const char* Version();
  public:
      sqlite_data Data;
      void Free();
      bool IsEmpty();
      bool Active();
      int RowCount();
      int ColCount();
  private:
      sqlite3 *m_db;
      char m_dbName[MAX_DBNAME];
//      static int sqlite_callback(void *param,int colcount,char **cols,char **colnames);
};
//---------------------------------------------------------------------------
static int sqlite_callback(void *param,int colcount,char **cols,char **colnames);
//---------------------------------------------------------------------------
#endif
/****************************************************************/
使用:
sqlite_db db;
db.Open("test.db");
db.ExecSQL("create table tmp(a INTEGER PRIMARY KEY,b int, c  char(1))");
db.ExecSQL("insert into tmp(b,c) select 1,'a'");

db.Query("select * from tmp");
for(int i=0; i<=db.RowCount(); i++)
{
for(int j=1; j<=db.ColCount(); j++)
{
printf("%s | ",db.Data(i,j));
}
printf("\n");
}

sqlite_data data;
db.Query(data,"select * from tmp");
for(int i=0; i<=data.RowCount(); i++)
{
for(int j=1; j<=data.ColCount(); j++)
{
printf("%s | ",data(i,j));
}
printf("\n");
}
db.Close();
 
本篇文章来源于 精品文章收藏网-IT技术宝藏 原文链接:
阅读(1348) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~