日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

sqlite3的backup和restore函数的使用

發布時間:2023/12/13 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 sqlite3的backup和restore函数的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

參考代碼

  • 第一段
  • 這個親測可以使用
#include <sqlite3.h> #include <iostream> /* ** Perform an online backup of database pDb to the database file named ** by zFilename. This function copies 5 database pages from pDb to ** zFilename, then unlocks pDb and sleeps for 250 ms, then repeats the ** process until the entire database is backed up. ** ** The third argument passed to this function must be a pointer to a progress ** function. After each set of 5 pages is backed up, the progress function ** is invoked with two integer parameters: the number of pages left to ** copy, and the total number of pages in the source file. This information ** may be used, for example, to update a GUI progress bar. ** ** While this function is running, another thread may use the database pDb, or ** another process may access the underlying database file via a separate ** connection. ** ** If the backup process is successfully completed, SQLITE_OK is returned. ** Otherwise, if an error occurs, an SQLite error code is returned. ** 官網地址:https://sqlite.org/backup.html */int backupDb(sqlite3 *pDb, /* Database to back up */const char *zFilename /* Name of file to back up to */ // void(*xProgress)(int, int) /* Progress function to invoke */ ){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);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, 5); // xProgress(sqlite3_backup_remaining(pBackup);sqlite3_backup_pagecount(pBackup); // );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; }
  • 第二段
  • 這個使用不順利,不知道是不是打開的方式不對
#pragma once#include <sqlite3.h> namespace hsm { namespace common {/*** @brief 備份和恢復數據庫數據* @param pInMemory 指向內存數據庫的指針* @param zFilename 指向文件數據庫目錄的字符串指針* @param isSave 0:從文件數據庫恢復到內存數據庫* 1:從內存數據庫備份到文件數據庫* @return 狀態碼*/ int backup_or_restore(sqlite3 *pInMemory,const char* zFilename,int isSave){int rc;sqlite3 *pFile;sqlite3_backup *pBackup;sqlite3 *pTo;sqlite3 *pFrom;rc = sqlite3_open(zFilename,&pFile);if (rc == SQLITE_OK){pFrom = (isSave?pInMemory:pFile);pTo = (isSave?pFile:pInMemory);pBackup = sqlite3_backup_init(pTo,"main",pFrom,"main");if (pBackup){(void)sqlite3_backup_step(pBackup,-1);(void)sqlite3_backup_finish(pBackup);}rc = sqlite3_close(pFile);}return rc; } } // namespace common } // namespace hsm

對于backup函數的調用例子

void KeyStorage::backup(const std::string &filename) {sqlite3 *pDB;int rc = sqlite3_open(MGMT_KEY_STORAGE_FILE, &pDB);if( rc==SQLITE_OK ){printf("open test.db OK!\n");rc = backupDb(pDB, MGMT_KEY_STORAGE_FILE);if( rc==SQLITE_OK ){printf("backupDb new.db OK!\n");} else {printf("backupDb new.db error!\n");}} else {printf("open test.db error!\n");} }

參考鏈接

  • https://sqlite.org/backup.html

總結

以上是生活随笔為你收集整理的sqlite3的backup和restore函数的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。