好久没有关注sqlite,这两天忽然想到它,就来看看,没想到sqlite都已经更新了好几版了。今天下了个最新的源代码,编译试试,发现编译dll后没lib文件,这让我有点郁闷,我再把以前我编译过的版本拿出来看看,靠,新的源码中竟然没提供def文件,怪不得搞不出lib文件。没撤,只好google一下怎么搞出lib文件,以下是一种方法我们可以照葫芦画瓢。
来源于:http://www.jb51.cc/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.lib和sqlite3.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://www.jb51.cc/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 CPPsqlITE_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); sqlite_int64 lastRowId(); void interrupt() { sqlite3_interrupt(mpDB); } void setBusyTimeout(int nMillisecs); static const char* sqliteVersion() { return sqlITE_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 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,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,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(CPPsqlITE_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) == sqlITE_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) == sqlITE_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) == sqlITE_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(CPPsqlITE_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) == sqlITE_NULL); } bool Cppsqlite3Query::fieldIsNull(const char* szField) { int nField = fieldIndex(szField); return (fieldDataType(nField) == sqlITE_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(CPPsqlITE_ERROR,"Invalid field name requested",DONT_DELETE_MSG); } const char* Cppsqlite3Query::fieldName(int nCol) { checkVM(); if (nCol < 0 || nCol > mnCols-1) { throw Cppsqlite3Exception(CPPsqlITE_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(CPPsqlITE_ERROR,DONT_DELETE_MSG); } return sqlite3_column_decltype(mpVM,nCol); } int Cppsqlite3Query::fieldDataType(int nCol) { checkVM(); if (nCol < 0 || nCol > mnCols-1) { throw Cppsqlite3Exception(CPPsqlITE_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 == sqlITE_DONE) { // no rows mbEof = true; } else if (nRet == sqlITE_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 != sqlITE_OK) { const char* szError = sqlite3_errmsg(mpDB); throw Cppsqlite3Exception(nRet,DONT_DELETE_MSG); } } } void Cppsqlite3Query::checkVM() { if (mpVM == 0) { throw Cppsqlite3Exception(CPPsqlITE_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(CPPsqlITE_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(CPPsqlITE_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(CPPsqlITE_ERROR,DONT_DELETE_MSG); } return mpaszResults[nCol]; } void Cppsqlite3Table::setRow(int nRow) { checkResults(); if (nRow < 0 || nRow > mnRows-1) { throw Cppsqlite3Exception(CPPsqlITE_ERROR,"Invalid row index requested",DONT_DELETE_MSG); } mnCurrentRow = nRow; } void Cppsqlite3Table::checkResults() { if (mpaszResults == 0) { throw Cppsqlite3Exception(CPPsqlITE_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 == sqlITE_DONE) { int nRowsChanged = sqlite3_changes(mpDB); nRet = sqlite3_reset(mpVM); if (nRet != sqlITE_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 == sqlITE_DONE) { // no rows return Cppsqlite3Query(mpDB,mpVM,true/*eof*/,false); } else if (nRet == sqlITE_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,sqlITE_TRANSIENT); if (nRes != sqlITE_OK) { throw Cppsqlite3Exception(nRes,"Error binding string param",const int nValue) { checkVM(); int nRes = sqlite3_bind_int(mpVM,nValue); if (nRes != sqlITE_OK) { throw Cppsqlite3Exception(nRes,"Error binding int param",const double dValue) { checkVM(); int nRes = sqlite3_bind_double(mpVM,dValue); if (nRes != sqlITE_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 != sqlITE_OK) { throw Cppsqlite3Exception(nRes,"Error binding NULL param",DONT_DELETE_MSG); } } void Cppsqlite3Statement::reset() { if (mpVM) { int nRet = sqlite3_reset(mpVM); if (nRet != sqlITE_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 != sqlITE_OK) { const char* szError = sqlite3_errmsg(mpDB); throw Cppsqlite3Exception(nRet,DONT_DELETE_MSG); } } } void Cppsqlite3Statement::checkDB() { if (mpDB == 0) { throw Cppsqlite3Exception(CPPsqlITE_ERROR,"Database not open",DONT_DELETE_MSG); } } void Cppsqlite3Statement::checkVM() { if (mpVM == 0) { throw Cppsqlite3Exception(CPPsqlITE_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 != sqlITE_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 == sqlITE_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 == sqlITE_DONE) { // no rows return Cppsqlite3Query(mpDB,pVM,true/*eof*/); } else if (nRet == sqlITE_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(CPPsqlITE_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 == sqlITE_OK) { return Cppsqlite3Table(paszResults,nRows,nCols); } else { throw Cppsqlite3Exception(nRet,szError); } } sqlite_int64 Cppsqlite3DB::lastRowId() { return sqlite3_last_insert_rowid(mpDB); } void Cppsqlite3DB::setBusyTimeout(int nMillisecs) { mnBusyTimeoutMs = nMillisecs; sqlite3_busy_timeout(mpDB,mnBusyTimeoutMs); } void Cppsqlite3DB::checkDB() { if (!mpDB) { throw Cppsqlite3Exception(CPPsqlITE_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 != sqlITE_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 required for an offset of 1 is N(1)+N(2)+N(40). The number ** of escapes required 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,具体哪个好,还没比较过,试试吧原文链接:https://www.f2er.com/sqlite/202393.html