sqlite实时备份

#include
#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;
}

你可能感兴趣的:(C++,sqlite,数据库,database)