C++ 将内存数据库保存到磁盘
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1437327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Saving to disk an in-memory database
提问by Giancarlo
I made a database through sqlite in c++.
我在 C++ 中通过 sqlite 创建了一个数据库。
The db has been created in memory (using the ":memory:" parameter insted of a filename), in order to have a very quick behavior.
db 已在内存中创建(使用文件名的“:memory:”参数),以便有一个非常快速的行为。
The database is created by the following lines:
数据库由以下几行创建:
sqlite3* mem_database;
if((SQLITE_OK == sqlite3_open(":memory:", &mem_database)){
// The db has been correctly created and
// I can do some stuff with it.
}
sqlite3_close(mem_database);
My problem is: how can I write the in-memory database to disk? (through c/c++ of course).
我的问题是:如何将内存数据库写入磁盘?(当然通过 c/c++)。
I read something about the ATTACH and DETACH sqlite commands, but I can get them working only with the sqlite interactive shell (not from c/c++ code).
我读了一些关于 ATTACH 和 DETACH sqlite 命令的内容,但我只能让它们与 sqlite 交互式 shell 一起工作(而不是来自 c/c++ 代码)。
Greets.
问候。
采纳答案by Nick Dandoulakis
Check out this example: Loading and Saving In-Memory Databases
查看此示例:加载和保存内存数据库
回答by nhaa123
Use transaction statement before doing anything to the table. This ensures fast handling and rollbacks as well. This way, you don't need to implement the database in memory directly.
在对表执行任何操作之前使用事务语句。这也确保了快速处理和回滚。这样,您就不需要直接在内存中实现数据库。
回答by Gupta
If you do not have enough time to read the whole documentation posted by @NickDandoulakis's answer, just copy and paste the below function (already mentioned in the link) in your code:
如果您没有足够的时间阅读@NickDandoulakis 的回答发布的整个文档,只需将以下函数(已在链接中提到)复制并粘贴到您的代码中:
int loadOrSaveDb(sqlite3 *pInMemory, const char *zFilename, int isSave)
{
int rc; /* Function return code */
sqlite3 *pFile; /* Database connection opened on zFilename */
sqlite3_backup *pBackup; /* Backup object used to copy data */
sqlite3 *pTo; /* Database to copy to (pFile or pInMemory) */
sqlite3 *pFrom; /* Database to copy from (pFile or pInMemory) */
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_errcode(pTo);
}
(void)sqlite3_close(pFile);
return rc;
}
and then for saving the data of your SQLite db (in memory) into a file call:
然后将 SQLite 数据库的数据(在内存中)保存到文件调用中:
loadOrSaveDb(db_con, target_file, 1);
Which db_con
is your database connection object pointer and target_file
is the target file address.
哪个db_con
是你的数据库连接对象指针,target_file
是目标文件地址。
Remark:If you want to load a SQLite database file into memory, just run:
备注:如果要将 SQLite 数据库文件加载到内存中,只需运行:
loadOrSaveDb(db_con, target_file, 0);