////////////////////////////////////////////////////////////////////////////////
// CppSQLite3 - A C++ wrapper around the SQLite3 embedded database library.
//
// Copyright (c) 2004 Rob Groves. All Rights Reserved. rob.groves@btinternet.com
//
// Permission to use, copy, modify, and distribute this software and its
// documentation for any purpose, without fee, and without a written
// agreement, is hereby granted, provided that the above copyright notice,
// this paragraph and the following two paragraphs appear in all copies,
// modifications, and distributions.
//
// IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
// INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST
// PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
// EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF
// ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". THE AUTHOR HAS NO OBLIGATION
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
//
// V3.0 03/08/2004 -Initial Version for sqlite3
//
// V3.1 16/09/2004 -Implemented getXXXXField using sqlite3 functions
// -Added CppSQLiteDB3::tableExists()
////////////////////////////////////////////////////////////////////////////////
#include "CppSQLite3.h"
#include <cstdlib>
// Named constant for passing to CppSQLite3Exception when passing it a string
// that cannot be deleted.
static const bool DONT_DELETE_MSG=false;
////////////////////////////////////////////////////////////////////////////////
// Prototypes for SQLite functions not included in SQLite DLL, but copied below
// from SQLite encode.c
////////////////////////////////////////////////////////////////////////////////
int sqlite3_encode_binary(const unsigned char *in, int n, unsigned char *out);
int sqlite3_decode_binary(const unsigned char *in, unsigned char *out);
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
CppSQLite3Exception::CppSQLite3Exception(const int nErrCode,
char* szErrMess,
bool bDeleteMsg/*=true*/) :
mnErrCode(nErrCode)
{
mpszErrMess = sqlite3_mprintf("%s[%d]: %s",
errorCodeAsString(nErrCode),
nErrCode,
szErrMess ? szErrMess : "");
if (bDeleteMsg && szErrMess)
{
sqlite3_free(szErrMess);
}
}
CppSQLite3Exception::CppSQLite3Exception(const CppSQLite3Exception& e) :
mnErrCode(e.mnErrCode)
{
mpszErrMess = 0;
if (e.mpszErrMess)
{
mpszErrMess = sqlite3_mprintf("%s", e.mpszErrMess);
}
}
const char* CppSQLite3Exception::errorCodeAsString(int nErrCode)
{
switch (nErrCode)
{
case SQLITE_OK : return "SQLITE_OK";
case SQLITE_ERROR : return "SQLITE_ERROR";
case SQLITE_INTERNAL : return "SQLITE_INTERNAL";
case SQLITE_PERM : return "SQLITE_PERM";
case SQLITE_ABORT : return "SQLITE_ABORT";
case SQLITE_BUSY : return "SQLITE_BUSY";
case SQLITE_LOCKED : return "SQLITE_LOCKED";
case SQLITE_NOMEM : return "SQLITE_NOMEM";
case SQLITE_READONLY : return "SQLITE_READONLY";
case SQLITE_INTERRUPT : return "SQLITE_INTERRUPT";
case SQLITE_IOERR : return "SQLITE_IOERR";
case SQLITE_CORRUPT : return "SQLITE_CORRUPT";
case SQLITE_NOTFOUND : return "SQLITE_NOTFOUND";
case SQLITE_FULL : return "SQLITE_FULL";
case SQLITE_CANTOPEN : return "SQLITE_CANTOPEN";
case SQLITE_PROTOCOL : return "SQLITE_PROTOCOL";
case SQLITE_EMPTY : return "SQLITE_EMPTY";
case SQLITE_SCHEMA : return "SQLITE_SCHEMA";
case SQLITE_TOOBIG : return "SQLITE_TOOBIG";
case SQLITE_CONSTRAINT : return "SQLITE_CONSTRAINT";
case SQLITE_MISMATCH : return "SQLITE_MISMATCH";
case SQLITE_MISUSE : return "SQLITE_MISUSE";
case SQLITE_NOLFS : return "SQLITE_NOLFS";
case SQLITE_AUTH : return "SQLITE_AUTH";
case SQLITE_FORMAT : return "SQLITE_FORMAT";
case SQLITE_RANGE : return "SQLITE_RANGE";
case SQLITE_ROW : return "SQLITE_ROW";
case SQLITE_DONE : return "SQLITE_DONE";
case CPPSQLITE_ERROR : return "CPPSQLITE_ERROR";
default: return "UNKNOWN_ERROR";
}
}
CppSQLite3Exception::~CppSQLite3Exception()
{
if (mpszErrMess)
{
sqlite3_free(mpszErrMess);
mpszErrMess = 0;
}
}
////////////////////////////////////////////////////////////////////////////////
CppSQLite3Buffer::CppSQLite3Buffer()
{
mpBuf = 0;
}
CppSQLite3Buffer::~CppSQLite3Buffer()
{
clear();
}
void CppSQLite3Buffer::clear()
{
if (mpBuf)
{
sqlite3_free(mpBuf);
mpBuf = 0;
}
}
const char* CppSQLite3Buffer::format(const char* szFormat, ...)
{
clear();
va_list va;
va_start(va, szFormat);
mpBuf = sqlite3_vmprintf(szFormat, va);
va_end(va);
return mpBuf;
}
////////////////////////////////////////////////////////////////////////////////
CppSQLite3Binary::CppSQLite3Binary() :
mpBuf(0),
mnBinaryLen(0),
mnBufferLen(0),
mnEncodedLen(0),
mbEncoded(false)
{
}
CppSQLite3Binary::~CppSQLite3Binary()
{
clear();
}
void CppSQLite3Binary::setBinary(const unsigned char* pBuf, int nLen)
{
mpBuf = allocBuffer(nLen);
memcpy(mpBuf, pBuf, nLen);
}
void CppSQLite3Binary::setEncoded(const unsigned char* pBuf)
{
clear();
mnEncodedLen = strlen((const char*)pBuf);
mnBufferLen = mnEncodedLen + 1; // Allow for NULL terminator
mpBuf = (unsigned char*)malloc(mnBufferLen);
if (!mpBuf)
{
throw CppSQLite3Exception(CPPSQLITE_ERROR,
"Cannot allocate memory",
DONT_DELETE_MSG);
}
memcpy(mpBuf, pBuf, mnBufferLen);
mbEncoded = true;
}
const unsigned char* CppSQLite3Binary::getEncoded()
{
if (!mbEncoded)
{
unsigned char* ptmp = (unsigned char*)malloc(mnBinaryLen);
memcpy(ptmp, mpBuf, mnBinaryLen);
mnEncodedLen = sqlite3_encode_binary(ptmp, mnBinaryLen, mpBuf);
free(ptmp);
mbEncoded = true;
}
return mpBuf;
}
const unsigned char* CppSQLite3Binary::getBinary()
{
if (mbEncoded)
{
// in/out buffers can be the same
mnBinaryLen = sqlite3_decode_binary(mpBuf, mpBuf);
if (mnBinaryLen == -1)
{
throw CppSQLite3Exception(CPPSQLITE_ERROR,
"Cannot decode binary",
DONT_DELETE_MSG);
}
mbEncoded = false;
}
return mpBuf;
}
int CppSQLite3Binary::getBinaryLength()
{
getBinary();
return mnBinaryLen;
}
unsigned char* CppSQLite3Binary::allocBuffer(int nLen)
{
clear();
// Allow extra space for encoded binary as per comments in
// SQLite encode.c See bottom of this file for implementation
// of SQLite functions use 3 instead of 2 just to be sure ;-)
mnBinaryLen = nLen;
mnBufferLen = 3 + (257*nLen)/254;
mpBuf = (unsigned char*)malloc(mnBufferLen);
if (!mpBuf)
{
throw CppSQLite3Exception(CPPSQLITE_ERROR,
"Cannot allocate memory",
DONT_DELETE_MSG);
}
mbEncoded = false;
return mpBuf;
}
void CppSQLite3Binary::clear()
{
if (mpBuf)
{
mnBinaryLen = 0;
mnBufferLen = 0;
free(mpBuf);
mpBuf = 0;
}
}
////////////////////////////////////////////////////////////////////////////////
CppSQLite3Query::CppSQLite3Query()
{
mpVM = 0;
mbEof = true;
mnCols = 0;
mbOwnVM = false;
}
CppSQLite3Query::CppSQLite3Query(const CppSQLite3Query& rQuery)
{
mpVM = rQuery.mpVM;
// Only one object can own the VM
const_cast<CppSQLite3Query&>(rQuery).mpVM = 0;
mbEof = rQuery.mbEof;
mnCols = rQuery.mnCols;
mbOwnVM = rQuery.mbOwnVM;
}
没有合适的资源?快使用搜索试试~ 我知道了~
用于读取SQLite书库的c++Lib
共21个文件
cpp:7个
h:3个
dsw:3个
3星 · 超过75%的资源 需积分: 16 8 下载量 128 浏览量
2010-03-24
16:49:13
上传
评论
收藏 205KB ZIP 举报
温馨提示
用于读取SQLite书库的c++Lib ++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++
资源推荐
资源详情
资源评论
收起资源包目录
CppSQLite_3_1(使用封装).zip (21个子文件)
3.1
ManagedDemo
ManagedDemo.vcproj 4KB
stdafx.h 166B
ManagedDemo.sln 911B
AssemblyInfo.cpp 2KB
stdafx.cpp 211B
ManagedDemo.cpp 10KB
CppSQLiteDemoExt
CppSQLiteDemoExt.dsp 4KB
CppSQLiteDemoExt.dsw 555B
CppSQLite3DemoExt.cpp 10KB
CppSQLiteDemoMT
CppSQLite3DemoMT.cpp 12KB
CppSQLiteDemoMT.dsw 553B
CppSQLiteDemoMT.dsp 4KB
CppSQLiteDemo
CppSQLiteDemo.dsw 549B
CppSQLite3Demo.cpp 10KB
CppSQLiteDemo.dsp 4KB
Common
CppSQLite3.h 7KB
sqlite3.h 49KB
sqlite3.dll 325KB
CppSQLite3.cpp 32KB
sqlite3.lib 24KB
sqlite3.def 2KB
共 21 条
- 1
资源评论
- 困困_BY2014-03-29还行,能用的
FROZEN_PEAK
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功