(From: http://blog.daum.net/_blog/BlogView.do?blogid=0Idq4&articleno=8429407#ajax_history_home)
??? "???? SQLite3 ??????? ????? ???? ??" ? ??? ???? ????.
[????]http://www.sqlite.org/backup.html
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <assert.h> 4 #include "sqlite3.h" 5 6 #pragma warning(disable:4996) 7 8 void Progress ( int nRemain, int nPageCount) 9 { 10 int nPercent = 0 ; 11 nPercent = 100 * ( nPageCount - nRemain) / nPageCount; 12 printf ( "Percent %d(%%) /n " , nPercent); 13 } 14 15 /** 16 @brief backup sqlite3 database 17 18 @param [ in] pDb Database to back up 19 @param [ in] zFilename Name of file to back up to 20 @param [out] xProgress Progress function to invoke 21 22 @return on case success 0 is returned, 23 otherwise see sqlite3.h error code 24 */ 25 int BackupDatabase ( sqlite3 * pDb, const char * zFilename, void (* xProgress)( int , int )) 26 { 27 int rc; /* Function return code */ 28 sqlite3 * pFile; /* Database connection opened on zFilename */ 29 sqlite3_backup * pBackup; /* Backup handle used to copy data */ 30 31 /* Open the database file identified by zFilename. */ 32 rc = sqlite3_open ( zFilename,& pFile); 33 if ( rc== SQLITE_OK ){ 34 35 /* Open the sqlite3_backup object used to accomplish the transfer */ 36 pBackup = sqlite3_backup_init ( pFile, "main" , pDb, "main" ); 37 if ( pBackup ){ 38 39 /* Each iteration of this loop copies 5 database pages from database 40 ** pDb to the backup database. If the return value of backup_step() 41 ** indicates that there are still further pages to copy,sleep for 42 ** 250 ms before repeating. */ 43 do { 44 rc = sqlite3_backup_step ( pBackup, 5 ); 45 xProgress ( 46 sqlite3_backup_remaining ( pBackup), 47 sqlite3_backup_pagecount ( pBackup) 48 ); 49 if ( rc== SQLITE_OK || rc== SQLITE_BUSY || rc== SQLITE_LOCKED ){ 50 sqlite3_sleep ( 250 ); 51 } 52 } while ( rc== SQLITE_OK || rc== SQLITE_BUSY || rc== SQLITE_LOCKED ); 53 54 /* Release resources allocated by backup_init(). */ 55 ( void ) sqlite3_backup_finish ( pBackup); 56 } 57 rc = sqlite3_errcode ( pFile); 58 } 59 60 /* Close the database connection opened on database file zFilename 61 ** and return the result of this function. */ 62 ( void ) sqlite3_close ( pFile); 63 return rc; 64 } 65 66 int main ( int argc, char ** argv) 67 { 68 sqlite3 * db = NULL; 69 70 assert ( sqlite3_open_v2 ( ":memory:" ,& db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL) == SQLITE_OK); 71 assert ( sqlite3_exec ( db, "create table address(id integer,name text);" , NULL, NULL) == SQLITE_OK); 72 assert ( sqlite3_exec ( db, "insert into address (id,name) values (1,'aa');" , NULL) == SQLITE_OK); 73 assert ( sqlite3_exec ( db,name) values (2,'bb');" , NULL) == SQLITE_OK); 74 assert ( sqlite3_exec ( db,name) values (3,'cc');" , NULL) == SQLITE_OK); 75 assert ( BackupDatabase ( db, "./test.db" , Progress) == SQLITE_OK); 76 assert ( sqlite3_close ( db) == SQLITE_OK); 77 78 return 0 ; 79 } (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|