Sqlite   发布时间:2022-05-22  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了关注SQLite大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

好久没有关注sqlite,这两天忽然想到它,就来看看,没想到sqlite都已经更新了好几版了。今天下了个最新的源代码,编译试试,发现编译dll后没lib文件,这让我有点郁闷,我再把以前我编译过的版本拿出来看看,靠,新的源码中竟然没提供def文件,怪不得搞不出lib文件。没撤,只好Google一下怎么搞出lib文件,以下是一种方法我们可以照葫芦画瓢。

来源于:http://code.js-code.com/article/p-yciibesb-bqv.html

VC中使用SQLite
sqlite官方下载只提供给我们一个sqlite3.dll跟一个sqlite3.def文件,并没有提供用于VC++6.0的lib文件,可以利用sqlite3.def文件生成,步骤如下:
1.将sqlite3.h(D:\sqlite-amalgamation-3_6_23.zip)拷贝到C:\Program files\Microsoft Visual studio\VC98\Include目录下,这时编译可通过,但链接错误,因为没有liB文件()
2.启动一个命令行程序,进入VC的安装目录C:\Program files\Microsoft Visual studio\VC98\Bin,在这个目录下面有一个liB.exe文件,使用它就能生成sqlite3.lib文件,将sqlite3.def文件放到相同目录,或者绝对路径也可以, 然后在命令行输入如下命令:
liB /MACHINE:IX86 /DEF:sqlite3.def
该命令生成两个文件:sqlite3.libsqlite3.exp
运行该命令时,如果提示找不到MSPDB60.DLL文件,可从其它目录拷贝至Bin目录下
3.将生成的sqlite3.lib拷贝到lib目录下,将sqlite3.dll拷贝到C:\WINNT\system32目录下
4.将sqlite3.lib加入到工程链接中,Project->SetTings,link选项卡,Object/library modules最后添入sqlite3.lib
D:\sqlitedll-3_6_23里有.dll和.def文件
生成的lib在bin下

这里还有一http://code.js-code.com/article/p-daizqzib-zk.html

sqlite毕竟是C的,我们使用起来比较麻烦不爽,现在都讲究面向对象,那我们就应该把它封装一下,唉,你想到的,别人早就做到了。这里有个sqlite C++的封装库,下载地址为http://www.sqlite.com.cn/Mysqlite/7/95.Html

////////////////////////////////////////////////////////////////////////////////
// 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()
////////////////////////////////////////////////////////////////////////////////
#ifndef _Cppsqlite3_H_
#define _Cppsqlite3_H_

#include "sqlite3.h"
#include <cstdio>
#include <cString>

#define CPP@R_749_5607@ERROR 1000

class Cppsqlite3Exception
{
public:

    Cppsqlite3Exception(const int nerrCode,char* szErrMess,bool bdeleteMsg=truE);

    Cppsqlite3Exception(const cppsqlite3Exception&  E);

    virtual ~Cppsqlite3Exception();

    const int errorCode() { return mnErrCode; }

    const char* errormessage() { return mpszErrMess; }

    static const char* errorCodeAsString(int nerrCodE);

private:

    int mnErrCode;
    char* mpszErrMess;
};


class Cppsqlite3Buffer
{
public:

    Cppsqlite3Buffer();

    ~Cppsqlite3Buffer();

    const char* format(const char* szFormat,...);

    operator const char*() { return mpBuf; }

    voID clear();

private:

    char* mpBuf;
};


class Cppsqlite3Binary
{
public:

    Cppsqlite3Binary();

    ~Cppsqlite3Binary();

    voID setBinary(const unsigned char* pBuf,int nLen);
    voID setEncoded(const unsigned char* pBuf);

    const unsigned char* getEncoded();
    const unsigned char* getBinary();

    int getBinaryLength();

    unsigned char* allocBuffer(int nLen);

    voID clear();

private:

    unsigned char* mpBuf;
    int mnBinaryLen;
    int mnBufferLen;
    int mnEncodedLen;
    bool mbEncoded;
};


class Cppsqlite3query
{
public:

    Cppsqlite3query();

    Cppsqlite3query(const cppsqlite3query& rquery);

    Cppsqlite3query(sqlite3* pDB,sqlite3_stmt* pVM,bool bEof,bool bOwnVM=truE);

    Cppsqlite3query& operator=(const cppsqlite3query& rquery);

    virtual ~Cppsqlite3query();

    int numFIElds();

    int fIEldindex(const char* szFIEld);
    const char* fIEldname(int nCol);

    const char* fIEldDeclType(int nCol);
    int fIEldDataType(int nCol);

    const char* fIEldValue(int nFIEld);
    const char* fIEldValue(const char* szFIEld);

    int geTintFIEld(int nFIEld,int nNullValue=0);
    int geTintFIEld(const char* szFIEld,int nNullValue=0);

    double getfloatFIEld(int nFIEld,double fNullValue=0.0);
    double getfloatFIEld(const char* szFIEld,double fNullValue=0.0);

    const char* getStringFIEld(int nFIEld,const char* szNullValue="");
    const char* getStringFIEld(const char* szFIEld,const char* szNullValue="");

    const unsigned char* getBlobFIEld(int nFIEld,int& nLen);
    const unsigned char* getBlobFIEld(const char* szFIEld,int& nLen);

    bool fIEldisNull(int nFIEld);
    bool fIEldisNull(const char* szFIEld);

    bool eof();

    voID nextRow();

    voID finalize();

private:

    voID checkVM();

	sqlite3* mpDB;
    sqlite3_stmt* mpVM;
    bool mbEof;
    int mnCols;
    bool mbOwnVM;
};


class Cppsqlite3table
{
public:

    Cppsqlite3table();

    Cppsqlite3table(const cppsqlite3table& rtablE);

    Cppsqlite3table(char** paszResults,int nRows,int nCols);

    virtual ~Cppsqlite3table();

    Cppsqlite3table& operator=(const cppsqlite3table& rtablE);

    int numFIElds();

    int numRows();

    const char* fIEldname(int nCol);

    const char* fIEldValue(int nFIEld);
    const char* fIEldValue(const char* szFIEld);

    int geTintFIEld(int nFIEld,const char* szNullValue="");

    bool fIEldisNull(int nFIEld);
    bool fIEldisNull(const char* szFIEld);

    voID setRow(int nRow);

    voID finalize();

private:

    voID checkResults();

    int mnCols;
    int mnRows;
    int mnCurrentRow;
    char** mpaszResults;
};


class Cppsqlite3Statement
{
public:

    Cppsqlite3Statement();

    Cppsqlite3Statement(const cppsqlite3Statement& rStatement);

    Cppsqlite3Statement(sqlite3* pDB,sqlite3_stmt* pVM);

    virtual ~Cppsqlite3Statement();

    Cppsqlite3Statement& operator=(const cppsqlite3Statement& rStatement);

    int execDML();

    Cppsqlite3query exeCQuery();

    voID bind(int nParam,const char* szvalue);
    voID bind(int nParam,const int nvalue);
    voID bind(int nParam,const double DWvalue);
    voID bind(int nParam,const unsigned char* blobValue,int nLen);
    voID bindNull(int nParam);

    voID reset();

    voID finalize();

private:

    voID checkDB();
    voID checkVM();

    sqlite3* mpDB;
    sqlite3_stmt* mpVM;
};


class Cppsqlite3DB
{
public:

    Cppsqlite3DB();

    virtual ~Cppsqlite3DB();

    voID open(const char* szfilE);

    voID close();

	bool tableExists(const char* sztablE);

    int execDML(const char* szSQL);

    Cppsqlite3query exeCQuery(const char* szSQL);

    int execScalar(const char* szSQL);

    Cppsqlite3table gettable(const char* szSQL);

    Cppsqlite3Statement compileStatement(const char* szSQL);

    @R_749_5607@int64 lastRowID();

    voID interrupt() { sqlite3_interrupt(mpDB); }

    voID setBusyTimeout(int nR_474_11845@illisecs);

    static const char* sqliteVersion() { return @R_749_5607@VERSION; }

private:

    Cppsqlite3DB(const cppsqlite3DB& db);
    Cppsqlite3DB& operator=(const cppsqlite3DB& db);

    sqlite3_stmt* compile(const char* szSQL);

    voID checkDB();

    sqlite3* mpDB;
    int mnBusyTimeoutms;
};

#endif


////////////////////////////////////////////////////////////////////////////////
// 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,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,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 @R_749_5607@OK          : return "@R_749_5607@OK";
		case @R_749_5607@ERROR       : return "@R_749_5607@ERROR";
		case @R_749_5607@INTERNAL    : return "@R_749_5607@INTERNAL";
		case @R_749_5607@PERM        : return "@R_749_5607@PERM";
		case @R_749_5607@ABORT       : return "@R_749_5607@ABORT";
		case @R_749_5607@BUSY        : return "@R_749_5607@BUSY";
		case @R_749_5607@LOCKED      : return "@R_749_5607@LOCKED";
		case @R_749_5607@NOMEM       : return "@R_749_5607@NOMEM";
		case @R_749_5607@Readonly    : return "@R_749_5607@Readonly";
		case @R_749_5607@INTERRUPT   : return "@R_749_5607@INTERRUPT";
		case @R_749_5607@IOERR       : return "@R_749_5607@IOERR";
		case @R_749_5607@CORRUPT     : return "@R_749_5607@CORRUPT";
		case @R_749_5607@NOTFOUND    : return "@R_749_5607@NOTFOUND";
		case @R_749_5607@FulL        : return "@R_749_5607@FulL";
		case @R_749_5607@CANtopEN    : return "@R_749_5607@CANtopEN";
		case @R_749_5607@PROTOCol    : return "@R_749_5607@PROTOCol";
		case @R_749_5607@EMPTY       : return "@R_749_5607@EMPTY";
		case @R_749_5607@scheR_474_11845@A      : return "@R_749_5607@scheR_474_11845@A";
		case @R_749_5607@TOOBIG      : return "@R_749_5607@TOOBIG";
		case @R_749_5607@CONSTraiNT  : return "@R_749_5607@CONSTraiNT";
		case @R_749_5607@mIsmaTCH    : return "@R_749_5607@mIsmaTCH";
		case @R_749_5607@mISUSE      : return "@R_749_5607@mISUSE";
		case @R_749_5607@NolFS       : return "@R_749_5607@NolFS";
		case @R_749_5607@AUTH        : return "@R_749_5607@AUTH";
		case @R_749_5607@FORMAT      : return "@R_749_5607@FORMAT";
		case @R_749_5607@RANGE       : return "@R_749_5607@RANGE";
		case @R_749_5607@ROW         : return "@R_749_5607@ROW";
		case @R_749_5607@DONE        : return "@R_749_5607@DONE";
		case CPP@R_749_5607@ERROR    : return "CPP@R_749_5607@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(CPP@R_749_5607@ERROR,"CAnnot allocate memory",DONT_deletE_MSG);
	}

	memcpy(mpBuf,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(CPP@R_749_5607@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(CPP@R_749_5607@ERROR,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;
}


Cppsqlite3query::Cppsqlite3query(sqlite3* pDB,bool bOwnVM/*=true*/)
{
	mpDB = pDB;
	mpVM = pVM;
	mbEof = bEof;
	mnCols = sqlite3_column_count(mpVM);
	mbOwnVM = bOwnVM;
}


Cppsqlite3query::~Cppsqlite3query()
{
	try
	{
		finalize();
	}
	catch (...)
	{
	}
}


Cppsqlite3query& Cppsqlite3query::operator=(const cppsqlite3query& rquery)
{
	try
	{
		finalize();
	}
	catch (...)
	{
	}
	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;
	return *this;
}


int Cppsqlite3query::numFIElds()
{
	checkVM();
	return mnCols;
}


const char* Cppsqlite3query::fIEldValue(int nFIEld)
{
	checkVM();

	if (nFIEld < 0 || nFIEld > mnCols-1)
	{
		throw Cppsqlite3Exception(CPP@R_749_5607@ERROR,"InvalID fIEld index requested",DONT_deletE_MSG);
	}

	return (const char*)sqlite3_column_text(mpVM,nFIEld);
}


const char* Cppsqlite3query::fIEldValue(const char* szFIEld)
{
	int nFIEld = fIEldindex(szFIEld);
	return (const char*)sqlite3_column_text(mpVM,nFIEld);
}


int Cppsqlite3query::geTintFIEld(int nFIEld,int nNullValue/*=0*/)
{
	if (fIEldDataType(nFIEld) == @R_749_5607@NulL)
	{
		return nNullValue;
	}
	else
	{
		return sqlite3_column_int(mpVM,nFIEld);
	}
}


int Cppsqlite3query::geTintFIEld(const char* szFIEld,int nNullValue/*=0*/)
{
	int nFIEld = fIEldindex(szFIEld);
	return geTintFIEld(nFIEld,nNullvalue);
}


double Cppsqlite3query::getfloatFIEld(int nFIEld,double fNullValue/*=0.0*/)
{
	if (fIEldDataType(nFIEld) == @R_749_5607@NulL)
	{
		return fNullValue;
	}
	else
	{
		return sqlite3_column_double(mpVM,nFIEld);
	}
}


double Cppsqlite3query::getfloatFIEld(const char* szFIEld,double fNullValue/*=0.0*/)
{
	int nFIEld = fIEldindex(szFIEld);
	return getfloatFIEld(nFIEld,fNullvalue);
}


const char* Cppsqlite3query::getStringFIEld(int nFIEld,const char* szNullValue/*=""*/)
{
	if (fIEldDataType(nFIEld) == @R_749_5607@NulL)
	{
		return szNullValue;
	}
	else
	{
		return (const char*)sqlite3_column_text(mpVM,nFIEld);
	}
}


const char* Cppsqlite3query::getStringFIEld(const char* szFIEld,const char* szNullValue/*=""*/)
{
	int nFIEld = fIEldindex(szFIEld);
	return getStringFIEld(nFIEld,szNullvalue);
}


const unsigned char* Cppsqlite3query::getBlobFIEld(int nFIEld,int& nLen)
{
	checkVM();

	if (nFIEld < 0 || nFIEld > mnCols-1)
	{
		throw Cppsqlite3Exception(CPP@R_749_5607@ERROR,DONT_deletE_MSG);
	}

	nLen = sqlite3_column_bytes(mpVM,nFIEld);
	return (const unsigned char*)sqlite3_column_blob(mpVM,nFIEld);
}


const unsigned char* Cppsqlite3query::getBlobFIEld(const char* szFIEld,int& nLen)
{
	int nFIEld = fIEldindex(szFIEld);
	return getBlobFIEld(nFIEld,nLen);
}


bool Cppsqlite3query::fIEldisNull(int nFIEld)
{
	return (fIEldDataType(nFIEld) == @R_749_5607@null);
}


bool Cppsqlite3query::fIEldisNull(const char* szFIEld)
{
	int nFIEld = fIEldindex(szFIEld);
	return (fIEldDataType(nFIEld) == @R_749_5607@null);
}


int Cppsqlite3query::fIEldindex(const char* szFIEld)
{
	checkVM();

	if (szFIEld)
	{
		for (int nFIEld = 0; nFIEld < mnCols; nFIEld++)
		{
			const char* szTemp = sqlite3_column_name(mpVM,nFIEld);

			if (strcmp(szFIEld,szTemp) == 0)
			{
				return nFIEld;
			}
		}
	}

	throw Cppsqlite3Exception(CPP@R_749_5607@ERROR,"InvalID fIEld name requested",DONT_deletE_MSG);
}


const char* Cppsqlite3query::fIEldname(int nCol)
{
	checkVM();

	if (nCol < 0 || nCol > mnCols-1)
	{
		throw Cppsqlite3Exception(CPP@R_749_5607@ERROR,DONT_deletE_MSG);
	}

	return sqlite3_column_name(mpVM,nCol);
}


const char* Cppsqlite3query::fIEldDeclType(int nCol)
{
	checkVM();

	if (nCol < 0 || nCol > mnCols-1)
	{
		throw Cppsqlite3Exception(CPP@R_749_5607@ERROR,DONT_deletE_MSG);
	}

	return sqlite3_column_decltype(mpVM,nCol);
}


int Cppsqlite3query::fIEldDataType(int nCol)
{
	checkVM();

	if (nCol < 0 || nCol > mnCols-1)
	{
		throw Cppsqlite3Exception(CPP@R_749_5607@ERROR,DONT_deletE_MSG);
	}

	return sqlite3_column_type(mpVM,nCol);
}


bool Cppsqlite3query::eof()
{
	checkVM();
	return mbEof;
}


voID Cppsqlite3query::nextRow()
{
	checkVM();

	int nRet = sqlite3_step(mpVM);

	if (nRet == @R_749_5607@DONE)
	{
		// no rows
		mbEof = true;
	}
	else if (nRet == @R_749_5607@ROW)
	{
		// more rows,nothing to do
	}
	else
	{
		nRet = sqlite3_finalize(mpVM);
		mpVM = 0;
		const char* szError = sqlite3_errmsg(mpDB);
		throw Cppsqlite3Exception(nRet,(char*)szError,DONT_deletE_MSG);
	}
}


voID Cppsqlite3query::finalize()
{
	if (mpVM && mbOwnVM)
	{
		int nRet = sqlite3_finalize(mpVM);
		mpVM = 0;
		if (nRet != @R_749_5607@OK)
		{
			const char* szError = sqlite3_errmsg(mpDB);
			throw Cppsqlite3Exception(nRet,DONT_deletE_MSG);
		}
	}
}


voID Cppsqlite3query::checkVM()
{
	if (mpVM == 0)
	{
		throw Cppsqlite3Exception(CPP@R_749_5607@ERROR,"Null Virtual Machine pointer",DONT_deletE_MSG);
	}
}


////////////////////////////////////////////////////////////////////////////////

Cppsqlite3table::Cppsqlite3table()
{
	mpaszResults = 0;
	mnRows = 0;
	mnCols = 0;
	mnCurrentRow = 0;
}


Cppsqlite3table::Cppsqlite3table(const cppsqlite3table& rtablE)
{
	mpaszResults = rtable.mpaszResults;
	// Only one object can own the results
	const_cast<Cppsqlite3table&>(rtablE).mpaszResults = 0;
	mnRows = rtable.mnRows;
	mnCols = rtable.mnCols;
	mnCurrentRow = rtable.mnCurrentRow;
}


Cppsqlite3table::Cppsqlite3table(char** paszResults,int nCols)
{
	mpaszResults = paszResults;
	mnRows = nRows;
	mnCols = nCols;
	mnCurrentRow = 0;
}


Cppsqlite3table::~Cppsqlite3table()
{
	try
	{
		finalize();
	}
	catch (...)
	{
	}
}


Cppsqlite3table& Cppsqlite3table::operator=(const cppsqlite3table& rtablE)
{
	try
	{
		finalize();
	}
	catch (...)
	{
	}
	mpaszResults = rtable.mpaszResults;
	// Only one object can own the results
	const_cast<Cppsqlite3table&>(rtablE).mpaszResults = 0;
	mnRows = rtable.mnRows;
	mnCols = rtable.mnCols;
	mnCurrentRow = rtable.mnCurrentRow;
	return *this;
}


voID Cppsqlite3table::finalize()
{
	if (mpaszResults)
	{
		sqlite3_free_table(mpaszResults);
		mpaszResults = 0;
	}
}


int Cppsqlite3table::numFIElds()
{
	checkResults();
	return mnCols;
}


int Cppsqlite3table::numRows()
{
	checkResults();
	return mnRows;
}


const char* Cppsqlite3table::fIEldValue(int nFIEld)
{
	checkResults();

	if (nFIEld < 0 || nFIEld > mnCols-1)
	{
		throw Cppsqlite3Exception(CPP@R_749_5607@ERROR,DONT_deletE_MSG);
	}

	int nIndex = (mnCurrentRow*mnCols) + mnCols + nFIEld;
	return mpaszResults[nIndex];
}


const char* Cppsqlite3table::fIEldValue(const char* szFIEld)
{
	checkResults();

	if (szFIEld)
	{
		for (int nFIEld = 0; nFIEld < mnCols; nFIEld++)
		{
			if (strcmp(szFIEld,mpaszResults[nFIEld]) == 0)
			{
				int nIndex = (mnCurrentRow*mnCols) + mnCols + nFIEld;
				return mpaszResults[nIndex];
			}
		}
	}

	throw Cppsqlite3Exception(CPP@R_749_5607@ERROR,DONT_deletE_MSG);
}


int Cppsqlite3table::geTintFIEld(int nFIEld,int nNullValue/*=0*/)
{
	if (fIEldisNull(nFIEld))
	{
		return nNullValue;
	}
	else
	{
		return atoi(fIEldValue(nFIEld));
	}
}


int Cppsqlite3table::geTintFIEld(const char* szFIEld,int nNullValue/*=0*/)
{
	if (fIEldisNull(szFIEld))
	{
		return nNullValue;
	}
	else
	{
		return atoi(fIEldValue(szFIEld));
	}
}


double Cppsqlite3table::getfloatFIEld(int nFIEld,double fNullValue/*=0.0*/)
{
	if (fIEldisNull(nFIEld))
	{
		return fNullValue;
	}
	else
	{
		return atof(fIEldValue(nFIEld));
	}
}


double Cppsqlite3table::getfloatFIEld(const char* szFIEld,double fNullValue/*=0.0*/)
{
	if (fIEldisNull(szFIEld))
	{
		return fNullValue;
	}
	else
	{
		return atof(fIEldValue(szFIEld));
	}
}


const char* Cppsqlite3table::getStringFIEld(int nFIEld,const char* szNullValue/*=""*/)
{
	if (fIEldisNull(nFIEld))
	{
		return szNullValue;
	}
	else
	{
		return fIEldValue(nFIEld);
	}
}


const char* Cppsqlite3table::getStringFIEld(const char* szFIEld,const char* szNullValue/*=""*/)
{
	if (fIEldisNull(szFIEld))
	{
		return szNullValue;
	}
	else
	{
		return fIEldValue(szFIEld);
	}
}


bool Cppsqlite3table::fIEldisNull(int nFIEld)
{
	checkResults();
	return (fIEldValue(nFIEld) == 0);
}


bool Cppsqlite3table::fIEldisNull(const char* szFIEld)
{
	checkResults();
	return (fIEldValue(szFIEld) == 0);
}


const char* Cppsqlite3table::fIEldname(int nCol)
{
	checkResults();

	if (nCol < 0 || nCol > mnCols-1)
	{
		throw Cppsqlite3Exception(CPP@R_749_5607@ERROR,DONT_deletE_MSG);
	}

	return mpaszResults[nCol];
}


voID Cppsqlite3table::setRow(int nRow)
{
	checkResults();

	if (nRow < 0 || nRow > mnRows-1)
	{
		throw Cppsqlite3Exception(CPP@R_749_5607@ERROR,"InvalID row index requested",DONT_deletE_MSG);
	}

	mnCurrentRow = nRow;
}


voID Cppsqlite3table::checkResults()
{
	if (mpaszResults == 0)
	{
		throw Cppsqlite3Exception(CPP@R_749_5607@ERROR,"Null Results pointer",DONT_deletE_MSG);
	}
}


////////////////////////////////////////////////////////////////////////////////

Cppsqlite3Statement::Cppsqlite3Statement()
{
	mpDB = 0;
	mpVM = 0;
}


Cppsqlite3Statement::Cppsqlite3Statement(const cppsqlite3Statement& rStatement)
{
	mpDB = rStatement.mpDB;
	mpVM = rStatement.mpVM;
	// Only one object can own VM
	const_cast<Cppsqlite3Statement&>(rStatement).mpVM = 0;
}


Cppsqlite3Statement::Cppsqlite3Statement(sqlite3* pDB,sqlite3_stmt* pVM)
{
	mpDB = pDB;
	mpVM = pVM;
}


Cppsqlite3Statement::~Cppsqlite3Statement()
{
	try
	{
		finalize();
	}
	catch (...)
	{
	}
}


Cppsqlite3Statement& Cppsqlite3Statement::operator=(const cppsqlite3Statement& rStatement)
{
	mpDB = rStatement.mpDB;
	mpVM = rStatement.mpVM;
	// Only one object can own VM
	const_cast<Cppsqlite3Statement&>(rStatement).mpVM = 0;
	return *this;
}


int Cppsqlite3Statement::execDML()
{
	checkDB();
	checkVM();

	const char* szError=0;

	int nRet = sqlite3_step(mpVM);

	if (nRet == @R_749_5607@DONE)
	{
		int nRowsChanged = sqlite3_changes(mpDB);

		nRet = sqlite3_reset(mpVM);

		if (nRet != @R_749_5607@OK)
		{
			szError = sqlite3_errmsg(mpDB);
			throw Cppsqlite3Exception(nRet,DONT_deletE_MSG);
		}

		return nRowsChanged;
	}
	else
	{
		nRet = sqlite3_reset(mpVM);
		szError = sqlite3_errmsg(mpDB);
		throw Cppsqlite3Exception(nRet,DONT_deletE_MSG);
	}
}


Cppsqlite3query Cppsqlite3Statement::exeCQuery()
{
	checkDB();
	checkVM();

	int nRet = sqlite3_step(mpVM);

	if (nRet == @R_749_5607@DONE)
	{
		// no rows
		return Cppsqlite3query(mpDB,mpVM,true/*eof*/,falsE);
	}
	else if (nRet == @R_749_5607@ROW)
	{
		// at least 1 row
		return Cppsqlite3query(mpDB,false/*eof*/,falsE);
	}
	else
	{
		nRet = sqlite3_reset(mpVM);
		const char* szError = sqlite3_errmsg(mpDB);
		throw Cppsqlite3Exception(nRet,DONT_deletE_MSG);
	}
}


voID Cppsqlite3Statement::bind(int nParam,const char* szvalue)
{
	checkVM();
	int nRes = sqlite3_bind_text(mpVM,nParam,szValue,-1,@R_749_5607@TRANSIENT);

	if (nRes != @R_749_5607@OK)
	{
		throw Cppsqlite3Exception(nRes,"Error binding String param",const int nvalue)
{
	checkVM();
	int nRes = sqlite3_bind_int(mpVM,nvalue);

	if (nRes != @R_749_5607@OK)
	{
		throw Cppsqlite3Exception(nRes,"Error binding int param",const double dvalue)
{
	checkVM();
	int nRes = sqlite3_bind_double(mpVM,dvalue);

	if (nRes != @R_749_5607@OK)
	{
		throw Cppsqlite3Exception(nRes,"Error binding double param",int nLen)
{
	checkVM();
	int nRes = sqlite3_bind_blob(mpVM,(const void*)blobValue,nLen,"Error binding blob param",DONT_deletE_MSG);
	}
}

	
voID Cppsqlite3Statement::bindNull(int nParam)
{
	checkVM();
	int nRes = sqlite3_bind_null(mpVM,nParam);

	if (nRes != @R_749_5607@OK)
	{
		throw Cppsqlite3Exception(nRes,"Error binding NulL param",DONT_deletE_MSG);
	}
}


voID Cppsqlite3Statement::reset()
{
	if (mpVM)
	{
		int nRet = sqlite3_reset(mpVM);

		if (nRet != @R_749_5607@OK)
		{
			const char* szError = sqlite3_errmsg(mpDB);
			throw Cppsqlite3Exception(nRet,DONT_deletE_MSG);
		}
	}
}


voID Cppsqlite3Statement::finalize()
{
	if (mpVM)
	{
		int nRet = sqlite3_finalize(mpVM);
		mpVM = 0;

		if (nRet != @R_749_5607@OK)
		{
			const char* szError = sqlite3_errmsg(mpDB);
			throw Cppsqlite3Exception(nRet,DONT_deletE_MSG);
		}
	}
}


voID Cppsqlite3Statement::checkDB()
{
	if (mpDB == 0)
	{
		throw Cppsqlite3Exception(CPP@R_749_5607@ERROR,"Database not open",DONT_deletE_MSG);
	}
}


voID Cppsqlite3Statement::checkVM()
{
	if (mpVM == 0)
	{
		throw Cppsqlite3Exception(CPP@R_749_5607@ERROR,DONT_deletE_MSG);
	}
}


////////////////////////////////////////////////////////////////////////////////

Cppsqlite3DB::Cppsqlite3DB()
{
	mpDB = 0;
	mnBusyTimeoutms = 60000; // 60 seconds
}


Cppsqlite3DB::Cppsqlite3DB(const cppsqlite3DB& db)
{
	mpDB = db.mpDB;
	mnBusyTimeoutms = 60000; // 60 seconds
}


Cppsqlite3DB::~Cppsqlite3DB()
{
	close();
}


Cppsqlite3DB& Cppsqlite3DB::operator=(const cppsqlite3DB& db)
{
	mpDB = db.mpDB;
	mnBusyTimeoutms = 60000; // 60 seconds
	return *this;
}


voID Cppsqlite3DB::open(const char* szfilE)
{
	int nRet = sqlite3_open(szfile,&mpDB);

	if (nRet != @R_749_5607@OK)
	{
		const char* szError = sqlite3_errmsg(mpDB);
		throw Cppsqlite3Exception(nRet,DONT_deletE_MSG);
	}

	setBusyTimeout(mnBusyTimeoutms);
}


voID Cppsqlite3DB::close()
{
	if (mpDB)
	{
		sqlite3_close(mpDB);
		mpDB = 0;
	}
}


Cppsqlite3Statement Cppsqlite3DB::compileStatement(const char* szSQL)
{
	checkDB();

	sqlite3_stmt* pVM = compile(szSQL);
	return Cppsqlite3Statement(mpDB,pVM);
}


bool Cppsqlite3DB::tableExists(const char* sztablE)
{
	char szSQL[128];
	sprintf(szSQL,"SELEct count(*) from sqlite_master where type='table' and name='%s'",sztablE);
	int nRet = execScalar(szSQL);
	return (nRet > 0);
}


int Cppsqlite3DB::execDML(const char* szSQL)
{
	checkDB();

	char* szError=0;

	int nRet = sqlite3_exec(mpDB,szSQL,&szError);

	if (nRet == @R_749_5607@OK)
	{
		return sqlite3_changes(mpDB);
	}
	else
	{
		throw Cppsqlite3Exception(nRet,szError);
	}
}


Cppsqlite3query Cppsqlite3DB::exeCQuery(const char* szSQL)
{
	checkDB();

	sqlite3_stmt* pVM = compile(szSQL);

	int nRet = sqlite3_step(pVM);

	if (nRet == @R_749_5607@DONE)
	{
		// no rows
		return Cppsqlite3query(mpDB,pVM,true/*eof*/);
	}
	else if (nRet == @R_749_5607@ROW)
	{
		// at least 1 row
		return Cppsqlite3query(mpDB,false/*eof*/);
	}
	else
	{
		nRet = sqlite3_finalize(pVM);
		const char* szError= sqlite3_errmsg(mpDB);
		throw Cppsqlite3Exception(nRet,DONT_deletE_MSG);
	}
}


int Cppsqlite3DB::execScalar(const char* szSQL)
{
	Cppsqlite3query q = exeCQuery(szSQL);

	if (q.eof() || q.numFIElds() < 1)
	{
		throw Cppsqlite3Exception(CPP@R_749_5607@ERROR,"InvalID scalar query",DONT_deletE_MSG);
	}

	return atoi(q.fIEldValue(0));
}


Cppsqlite3table Cppsqlite3DB::gettable(const char* szSQL)
{
	checkDB();

	char* szError=0;
	char** paszResults=0;
	int nRet;
	int nRows(0);
	int nCols(0);

	nRet = sqlite3_get_table(mpDB,&paszResults,&nrows,&nCols,&szError);

	if (nRet == @R_749_5607@OK)
	{
		return Cppsqlite3table(paszResults,nRows,nCols);
	}
	else
	{
		throw Cppsqlite3Exception(nRet,szError);
	}
}


@R_749_5607@int64 Cppsqlite3DB::lastRowID()
{
	return sqlite3_last_insert_rowID(mpDB);
}


voID Cppsqlite3DB::setBusyTimeout(int nR_474_11845@illisecs)
{
	mnBusyTimeoutms = nMillisecs;
	sqlite3_busy_timeout(mpDB,mnBusyTimeoutms);
}


voID Cppsqlite3DB::checkDB()
{
	if (!mpDB)
	{
		throw Cppsqlite3Exception(CPP@R_749_5607@ERROR,DONT_deletE_MSG);
	}
}


sqlite3_stmt* Cppsqlite3DB::compile(const char* szSQL)
{
	checkDB();

	char* szError=0;
	const char* szTail=0;
	sqlite3_stmt* pVM;

	int nRet = sqlite3_prepare(mpDB,&pVM,&szTail);

	if (nRet != @R_749_5607@OK)
	{
		throw Cppsqlite3Exception(nRet,szError);
	}

	return pVM;
}


////////////////////////////////////////////////////////////////////////////////
// sqlite encode.c reproduced here,containing implementation notes and source
// for sqlite3_encode_binary() and sqlite3_decode_binary() 
////////////////////////////////////////////////////////////////////////////////

/*
** 2002 April 25
**
** The author disclaims copyright to this source code.  In place of
** a legal notice,here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely,never taking more than you give.
**
*************************************************************************
** This file contains Helper routInes used to translate binary data into
** a null-terminated String (suitable for use in sqlitE) and BACk again.
** these are convenIEnce routInes for use by people who want to store binary
** data in an sqlite database.  The code in this file is not used by any other
** part of the sqlite library.
**
** $ID: encode.c,v 1.10 2004/01/14 21:59:23 drh Exp $
*/

/*
** How This Encoder Works
**
** The output is allowed to contain any character except 0x27 (') and
** 0x00.  This is accomplished by using an escape character to encode
** 0x27 and 0x00 as a two-byte sequence.  The escape character is always
** 0x01.  An 0x00 is encoded as the two byte sequence 0x01 0x01.  The
** 0x27 character is encoded as the two byte sequence 0x01 0x03.  Finally,** the escape character itself is encoded as the two-character sequence
** 0x01 0x02.
**
** To summarize,the encoder works by using an escape sequences as follows:
**
**       0x00  ->  0x01 0x01
**       0x01  ->  0x01 0x02
**       0x27  ->  0x01 0x03
**
** If that were all the encoder dID,it would work,but in certain cases
** it Could double the size of the encoded String.  For example,to
** encode a String of 100 0x27 characters would require 100 instances of
** the 0x01 0x03 escape sequence resulTing in a 200-character output.
** We would prefer to keep the size of the encoded String smaller than
** this.
**
** To minimize the enCoding size,we first add a fixed offset value to each 
** byte in the sequence.  The addition is modulo 256.  (That is to say,if
** the sum of the original character value and the offset exceeds 256,then
** the higher order bits are truncated.)  The offset is chosen to minimize
** the number of characters in the String that need to be escaped.  For
** example,in the case above where the String was composed of 100 0x27
** characters,the offset might be 0x01.  Each of the 0x27 characters would
** then be converted into an 0x28 character which would not need to be
** escaped at all and so the 100 character input String would be converted
** into just 100 characters of output.  Actually 101 characters of output - 
** we have to record the offset used as the first byte in the sequence so
** that the String can be decoded.  Since the offset value is stored as
** part of the output String and the output String is not allowed to contain
** characters 0x00 or 0x27,the offset cAnnot be 0x00 or 0x27.
**
** Here,then,are the enCoding steps:
**
**     (1)   Choose an offset value and make it the first character of
**           output.
**
**     (2)   copy each input character into the output buffer,one by
**           one,adding the offset value as you copy.
**
**     (3)   If the value of an input character plus offset is 0x00,replace
**           that one character by the two-character sequence 0x01 0x01.
**           If the sum is 0x01,replace it with 0x01 0x02.  If the sum
**           is 0x27,replace it with 0x01 0x03.
**
**     (4)   Put a 0x00 terminator at the end of the output.
**
** DeCoding is obvIoUs:
**
**     (5)   copy encoded characters except the first into the decode 
**           buffer.  Set the first encoded character asIDe for use as
**           the offset in step 7 below.
**
**     (6)   Convert each 0x01 0x01 sequencE into a single character 0x00.
**           Convert 0x01 0x02 into 0x01.  Convert 0x01 0x03 into 0x27.
**
**     (7)   Subtract the offset value that was the first character of
**           the encoded buffer from all characters in the output buffer.
**
** The only tricky part is step (1) - how to compute an offset value to
** minimize the size of the output buffer.  This is accomplished by tesTing
** all offset values and picking the one that results in the feWest number
** of escapes.  To do that,we first scan the entire input and count the
** number of occurances of each character value in the input.  Suppose
** the number of 0x00 characters is N(0),the number of occurances of 0x01
** is N(1),and so forth up to the number of occurances of 0xff is N(255).
** An offset of 0 is not allowed so we don't have to test it.  the number
** of escapes @R_502_4173@ for an offset of 1 is N(1)+N(2)+N(40).  the number
** of escapes @R_502_4173@ for an offset of 2 is N(2)+N(3)+N(41).  And so forth.
** In this way we find the offset that gives the minimum number of escapes,** and thus minimizes the length of the output String.
*/

/*
** Encode a binary buffer "in" of size n bytes so that it contains
** no instances of characters '\'' or '\000'.  The output is 
** null-terminated and can be used as a String value in an INSERT
** or updatE statement.  Use sqlite3_decode_binary() to convert the
** String BACk into its original binary.
**
** The result is written into a preallocated output buffer "out".
** "out" must be able to hold at least 2 +(257*n)/254 bytes.
** In other words,the output will be expanded by as much as 3
** bytes for every 254 bytes of input plus 2 bytes of fixed overhead.
** (This is approximately 2 + 1.0118*n or about a 1.2% size increase.)
**
** The return value is the number of characters in the encoded
** String,excluding the "\000" terminator.
*/
int sqlite3_encode_binary(const unsigned char *in,unsigned char *out){
  int i,j,e,m;
  int cnt[256];
  if( n<=0 ){
    out[0] = 'x';
    out[1] = 0;
    return 1;
  }
  memset(cnt,sizeof(cnt));
  for(i=n-1; i>=0; i--){ cnt[in[i]]++; }
  m = n;
  for(i=1; i<256; i++){
    int sum;
    if( i=='\'' ) conTinue;
    sum = cnt[i] + cnt[(i+1)&0xff] + cnt[(i+'\'')&0xff];
    if( sum<m ){
      m = sum;
      e = i;
      if( m==0 ) break;
    }
  }
  out[0] = e;
  j = 1;
  for(i=0; i<n; i++){
    int c = (in[i] - E)&0xff;
    if( c==0 ){
      out[j++] = 1;
      out[j++] = 1;
    }else if( c==1 ){
      out[j++] = 1;
      out[j++] = 2;
    }else if( c=='\'' ){
      out[j++] = 1;
      out[j++] = 3;
    }else{
      out[j++] = c;
    }
  }
  out[j] = 0;
  return j;
}

/*
** Decode the String "in" into binary data and write it into "out".
** This routIne reverses the enCoding created by sqlite3_encode_binary().
** The output will always be a few bytes less than the input.  the number
** of bytes of output is returned.  If the input is not a well-formed
** enCoding,-1 is returned.
**
** The "in" and "out" parameters may point to the same buffer in order
** to decode a String in place.
*/
int sqlite3_decode_binary(const unsigned char *in,c,e;
  e = *(in++);
  i = 0;
  while( (c = *(in++))!=0 ){
    if( c==1 ){
      c = *(in++);
      if( c==1 ){
        c = 0;
      }else if( c==2 ){
        c = 1;
      }else if( c==3 ){
        c = '\'';
      }else{
        return -1;
      }
    }
    out[i++] = (c + E)&0xff;
  }
  return i;
}
这里还有一个外国哥们写的一个sqlite C++类http://www.adp-gmbh.ch/sqlite/wrapper.html,具体哪个好,还没比较过,试试吧

大佬总结

以上是大佬教程为你收集整理的关注SQLite全部内容,希望文章能够帮你解决关注SQLite所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。