sqlite实时备份

#include <psapi.h>
#pragma comment(lib,"psapi.lib")

int CAceaDbProc::BackupDB(sqlite3* pDb, const char* zFilename, void(*xProgress)(int, int))
{
    HANDLE handle = GetCurrentProcess();
    PROCESS_MEMORY_COUNTERS pmc;
    CString strLog = NULLSTR;

    const int iConversionRate = 1024 * 1024;
    const int iPageCountEachLoop = 10000;

    int rc;                     /* Function return code */
    sqlite3* pFile;             /* Database connection opened on zFilename */
    sqlite3_backup* pBackup;    /* Backup handle used to copy data */

    /* Open the database file identified by zFilename. */
    rc = sqlite3_open(zFilename, &pFile);

    CString strPassword = "AceASyS";
    rc = sqlite3_key(pFile, strPassword, strPassword.GetLength());

    if (rc == SQLITE_OK) {

        /* Open the sqlite3_backup object used to accomplish the transfer */
        pBackup = sqlite3_backup_init(pFile, "main", pDb, "main");
        if (pBackup) {

            /* Each iteration of this loop copies 5 database pages from database
            ** pDb to the backup database. If the return value of backup_step()
            ** indicates that there are still further pages to copy, sleep for
            ** 250 ms before repeating. */
            do {
                rc = sqlite3_backup_step(pBackup, iPageCountEachLoop);
                xProgress(
                    sqlite3_backup_remaining(pBackup),
                    sqlite3_backup_pagecount(pBackup)
                );

                //获取目前进程内存+目标文件大小,实时查看
                GetProcessMemoryInfo(handle, &pmc, sizeof(pmc));

                double dFileZize = CHelper::GetFileSize(zFilename, 1);
                strLog.Format("内存使用:%d MB,%d MB,%d MB,%d MB, 目标文件大小: %f KB\n",
                    pmc.WorkingSetSize / iConversionRate, pmc.PeakWorkingSetSize / iConversionRate,
                    pmc.PagefileUsage / iConversionRate, pmc.PeakPagefileUsage / iConversionRate, dFileZize);
                CImageHelper::SaveToLogFile("DBBackupLog.txt", strLog);
                //

                if (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED) {
                    sqlite3_sleep(250);
                }
            } while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED);

            /* Release resources allocated by backup_init(). */
            (void)sqlite3_backup_finish(pBackup);
        }
        rc = sqlite3_errcode(pFile);
    }

    /* Close the database connection opened on database file zFilename
    ** and return the result of this function. */
    (void)sqlite3_close(pFile);

    return rc;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值