设为首页 加入收藏

TOP

C++使用Sqlite3,使用CppSQLite3的封装(九)
2016-04-27 17:25:21 】 浏览:2642
Tags:使用 Sqlite3 CppSQLite3 封装
rator=(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, (char*)szError, DONT_DELETE_MSG); } setBusyTimeout(mnBusyTimeoutMs); } void CppSQLite3DB::close() { if (mpDB) { if (sqlite3_close(mpDB) == SQLITE_OK) { mpDB = 0; } else { throw CppSQLite3Exception(CPPSQLITE_ERROR, "Unable to close database", DONT_DELETE_MSG); } } } CppSQLite3Statement CppSQLite3DB::compileStatement(const char* szSQL) { checkDB(); sqlite3_stmt* pVM = compile(szSQL); return CppSQLite3Statement(mpDB, pVM); } bool CppSQLite3DB::tableExists(const char* szTable) { char szSQL[256]; 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, 0, 0, &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, pVM, false/*eof*/); } else { nRet = sqlite3_finalize(pVM); const char* szError= sqlite3_errmsg(mpDB); throw CppSQLite3Exception(nRet, (char*)szError, DONT_DELETE_MSG); } } int CppSQLite3DB::execScalar(const char* szSQL, int nNullValue/*=0*/) { CppSQLite3Query q = execQuery(szSQL); if (q.eof() || q.numFields() < 1) { throw CppSQLite3Exception(CPPSQLITE_ERROR, "Invalid scalar query", DONT_DELETE_MSG); } return q.getIntField(0, nNullValue); } 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, szSQL, &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, "Database not open", DONT_DELETE_MSG); } } sqlite3_stmt* CppSQLite3DB::compile(const char* szSQL) { checkDB(); const char* szTail=0; sqlite3_stmt* pVM; int nRet = sqlite3_prepare_v2(mpDB, szSQL, -1, &pVM, &szTail); if (nRet != SQLITE_OK) { const char* szError = sqlite3_errmsg(mpDB); throw CppSQLite3Exception(nRet, (char*)szError, DONT_DELETE_MSG); } return pVM; } bool CppSQLite3DB::IsAutoCommitOn() { checkDB(); return sqlite3_get_autocommit(mpDB) true : false; } //////////////////////////////////////////////////////////////////////////////// // 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 char
首页 上一页 6 7 8 9 10 下一页 尾页 9/10/10
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C++11新特性 下一篇c++内存管理

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目