(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 }