设为首页 加入收藏

TOP

SQLite学习手册(在线备份) (二)
2014-11-24 08:24:01 来源: 作者: 【 】 浏览:4
Tags:SQLite 学习 手册 在线 备份
successfully created, call backup_step()
44 ** to copy data from pFile to pInMemory. Then call backup_finish()
45 ** to release resources associated with the pBackup object. If an
46 ** error occurred, then an error code and message will be left in
47 ** connection pTo. If no error occurred, then the error code belonging
48 ** to pTo is set to SQLITE_OK.
49 */
50 pBackup = sqlite3_backup_init(pTo, "main", pFrom, "main");
51 if( pBackup ){
52 (void)sqlite3_backup_step(pBackup, -1);
53 (void)sqlite3_backup_finish(pBackup);
54 }
55 rc = sqlite3_errcode(pTo);
56 }
57
58 /* Close the database connection opened on database file zFilename
59 ** and return the result of this function. */
60 (void)sqlite3_close(pFile);
61 return rc;
62 }

三、高级应用技巧:

在上面的例子中,我们是通过sqlite3_backup_step()函数的一次调用完成了整个拷贝过程。该实现方式仍然存在之前说过的挂起其它写访问连接的问题,为了解决该问题,这里我们将继续介绍另外一种更高级的实现方式--分片拷贝,其实现步骤如下:
1). 函数sqlite3_backup_init()用于创建sqlite3_backup对象,该对象将作为本次拷贝操作的句柄传给其余两个函数。
2). 函数sqlite3_backup_step()被调用用于拷贝数据,和之前方法不同的是,该函数的第二个参数不再是-1,而是一个普通的正整数,表示每次调用将会拷贝的页面数量,如5。
3). 如果在函数sqlite3_backup_step()调用结束后,仍然有更多的页面需要被拷贝,那么我们将主动休眠250ms,然后再重复步骤2).
4). 函数sqlite3_backup_finish()用于释放sqlite3_backup_init()函数申请的资源,以避免资源泄露。
在上述步骤3)中我们主动休眠250ms,此期间,该拷贝操作不会在源数据库上持有任何读锁,这样其它的数据库连接在进行写操作时亦将不会被挂起。然而在休眠期间,如果另外一个线程或进程对源数据库进行了写操作,SQLite将会检测到该事件的发生,从而在下一次调用sqlite3_backup_step()函数时重新开始整个拷贝过程。唯一的例外是,如果源数据库不是in-memory数据库,同时写操作是在与拷贝操作同一个进程内完成,并且在操作时使用的也是同一个数据库连接句柄,那么目的数据库中数据也将被此操作同时自动修改。在下一次调用sqlite3_backup_step()时,也将不会有任何影响发生。  
事实上,在SQLite中仍然提供了另外两个辅助性函数backup_remaining()和backup_pagecount(),其中前者将返回在当前备份操作中还有多少页面需要被拷贝,而后者将返回本次操作总共需要拷贝的页面数量。显而易见的是,通过这两个函数的返回结果,我们可以实时显示本次备份操作的整体进度,计算公式如下:
Completion = 100% * (pagecount() - remaining()) / pagecount()
见以下代码示例(来自SQLite官网):
1 /*
2 ** Perform an online backup of database pDb to the database file named
3 ** by zFilename. This function copies 5 database pages from pDb to
4 ** zFilename, then unlocks pDb and sleeps for 250 ms, then repeats the
5 ** process until the entire database is backed up.
6 **
7 ** The third argument passed to this function must be a pointer to a progress
8 ** function. After each set of 5 pages is backed up, the progress function
9 ** is invoked with two integer parameters: the number of pages left to
10 ** copy, and the total number of pages in the source file. This information
11 ** may be used, for example, to update a GUI progress bar.
12 **
13 ** While this function is running, another thread may use the database pDb, or
14 ** another process may access the underlying database file via a separate
15 ** connection.
16 **
17 ** If the backup process is successfully completed, SQLITE_OK is returned.
18 ** Otherwise, if an error occurs, an SQLite error code is returned.
19 */
20 int backupDb(
21 sqlite3 *pDb, /* Database to back up */
22 const char *zFilename, /* Name of file to back up to */
23 void(*xProgress)(int, int) /* Progress function to invoke */
24 ){
25 int rc; /* Function return code */
26 sqlite3 *pFile; /* Database connection opened on zFilename */
27 sqlite3_backup *pBackup; /* Backup handle used to copy data */
28
29 /* Open the database file identified by zFilename. */
30 rc = sqlite3_open(zFilename, &pFile);
31 if( rc==SQLITE_OK ){
32
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇PostgreSQL启动过程中的那些事七.. 下一篇mongodb date type

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis压力测试实战 - (2025-12-27 09:20:24)
·高并发一上来,微服 (2025-12-27 09:20:21)
·Redis 高可用架构深 (2025-12-27 09:20:18)
·Linux 系统监控 的完 (2025-12-27 08:52:29)
·一口气总结,25 个 L (2025-12-27 08:52:27)