设为首页 加入收藏

TOP

C++使用Sqlite3,使用CppSQLite3的封装(十)
2016-04-27 17:25:21 】 浏览:2632
Tags:使用 Sqlite3 CppSQLite3 封装
acter 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, int n, unsigned char *out){ int i, j, e, m; int cnt[256]; if( n<=0 ){ out[0] = 'x'; out[1] = 0; return 1; } memset(cnt, 0, 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

使用方法

#include 
    
      #include "CppSQLite3.h" #include 
     
       using namespace std; const char * gszFile = "e:\\sqlite3\\test.db"; //测试Sqlite3的使用 void main1() { cout << "hello sqlite3" << endl; try { int i, fld; time_t tmStart, tmEnd; CppSQLite3DB db; char *sql; cout << "SQLite Header Version: " << CppSQLite3DB::SQLiteHeaderVersion() << endl; cout << "SQLite Library Version: " << CppSQLite3DB::SQLiteLibraryVersion() << endl; cout << "SQLite Library Version Number: " << CppSQLite3DB::SQLiteLibraryVersionNumber() << endl; //remove(gszFile); db.open(gszFile); //插入数据 cout << endl << "emp table exists=" << (db.tableExists("person")  "TRUE" : "FALSE") << endl; // Create SQL statement sql = "CREATE TABLE person(" \ "id INT PRIMARY KEY NOT NULL," \ "name TEXT NOT NULL," \ "age INT NOT NULL," \ "address CHAR(50)," \ "salary REAL );"; //db.execDML(sql); //cout << endl << "emp table exists=" << (db.tableExists("person")  "TRUE" : "FALSE") << endl; //cout << endl << "DML tests" << endl; sql = "insert into person values (1, 'David Beckham',34,'china person',3455.78);"\ "insert into person values (2, 'changshuhang',33,'china person1',3455.34);"\ "insert into person values (3, 'houyuzhu',37,'china person2',34534);"\ "insert into person values (4, 'chengye',356,'china person3',45.78);"\ "insert into person values (5, 'dujianfei',38,'china person4',3655.78);"; int nRows = db.execDML(sql); cout << nRows << " rows inserted" << endl; //update /* sql = "update person set name = 'xijinping' where id = 5 ;"\ "update person set name = 'hujingtao' where address = 'china person2' ;"; int nRows = db.execDML(sql); cout << nRows << " rows updated" << endl; */ //delete /* sql = "delete from person where id = 5 ;"\ "delete from person where address = 'china person2' ;"; int nRows = db.execDML(sql); cout << nRows << " rows updated" << endl; */ /* // Query data and also show results of inserts into auto-increment field cout << endl << "Select statement test" << endl; CppSQLite3Query q = db.execQuery("select * from person order by id;"); for (fld = 0; fld < q.numFields(); fld++) { cout << q.fieldName(fld) << "(" << q.fieldDeclType(fld) << ")|"; } cout << endl; while (!q.eof()) { cout << q.fieldValue(0) << "|"; cout << q.fieldValue(1) << "|"; cout << q.fieldValue(2) << "|"; cout << q.fieldValue(3) << endl; q.nextRow(); } */ /* // Fetch table at once, and also show how to use CppSQLiteTable::setRow() method cout << endl << "getTable() test" << endl; CppSQLite3Table t = db.getTable("select * from person order by id;"); for (fld = 0; fld < t.numFields(); fld++) { cout << t.fieldName(fld) << "|"; } cout << endl; for (int row = 0; row < t.numRows(); row++) { t.setRow(row); for (int fld = 0; fld < t.numFields(); fld++) { if (!t.fieldIsNull(fld)) cout << t.fieldValue(fld) << "|"; else cout << "NULL" << "|"; } cout << endl; } */ } catch (CppSQLite3Exception& e) { cerr << e.errorCode() << ":" << e.errorMessage() << endl; } //////////////////////////////////////////////////////////////////////////////// // Loop until user enters q or Q //////////////////////////////////////////////////////////////////////////////// char c(' '); while (c != 'q' && c != 'Q') { cout << "Press q then enter to quit: "; cin >> c; } }
     
    

首页 上一页 7 8 9 10 下一页 尾页 10/10/10
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C++11新特性 下一篇c++内存管理

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目