将 SQLite 数据库复制到另一个路径 [英] Copy SQLite database to another path

查看:28
本文介绍了将 SQLite 数据库复制到另一个路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在临时文件夹中创建一个 sqlite 数据库.现在我想将该文件复制到另一个文件夹.是否有任何sqlite命令可以重命名sqlite数据库文件?

我尝试在 c++ 中使用 rename 函数,但它返回错误 18.错误号 18 表示:包含名称 newname 的目录必须与文件位于同一文件系统(如名称 oldname 所示)".

有人可以提出更好的方法来做到这一点.

解决方案

在正确的文件系统上使用临时目录!

首先,sqlite 数据库只是一个文件.可以随心所欲地移动或复制它,前提是:

  • 上次正确关闭,因此日志中没有可回滚的内容
  • 如果它使用预写日志类型的日志,则它是完全检查点的.

因此将其作为文件移动是正确的.现在有两种方法可以移动文件:

  • 使用 rename 系统调用.
  • 通过复制文件并删除旧文件.

前者有很多优点:它不能留下部分写入的文件,不能留下两个文件,速度非常快,如果你用它来重命名旧文件,则目标没有周期name 不存在(最后一个是 POSIX 语义,Windows 可以在 NTFS 上执行,但不能在 FAT 文件系统上执行).它还有一个重要的缺点:它只能在文件系统中工作.所以你必须:

  • 如果您重命名它以确保在进程失败时不会留下部分写入的文件,则需要使用重命名,因此必须使用同一文件系统上的临时位置.通常这是通过在同一目录中使用不同的名称而不是使用临时目录来完成的.
  • 如果您重命名它是因为目的地可能很慢,例如因为你想把它放在网络共享上,你显然想在不同的文件系统上使用临时目录.这意味着您必须读取该文件并将其写入新名称.在标准的 C 或 C++ 库中没有这方面的功能,但许多库都有一个(像 python 这样的高级语言也有一个,你总是可以执行 /bin/mv 来做给你).

I am creating a sqlite database in temp folder. Now I want to copy that file to another folder. Is there any sqlite command to rename the sqlite database file?

I tried using rename function in c++ but it returns error 18. Error no 18 means: "The directory containing the name newname must be on the same file system as the file (as indicated by the name oldname)".

Can someone suggest a better way to do this.

解决方案

Use a temporary directory on the correct filesystem!

First, sqlite database is just a file. It can be moved or copied around whatever you wish, provided that:

  • It was correctly closed last time, so there would be no stuff to roll-back in the journal
  • If it uses write-ahead log type of journal, it is fully checkpointed.

So moving it as a file is correct. Now there are two ways to move a file:

  • Using the rename system call.
  • By copying the file and deleting the old one.

The former has many advantages: it can't leave partially written file around, it can't leave both files around , it is very fast and if you use it to rename over old file, there is no period where the target name wouldn't exist (the last is POSIX semantics and Windows can do it on NTFS, but not FAT filesystem). It also has one important disadvantage: it only works within the filesystem. So you have to:

  • If you are renaming it to ensure that a partially written file is not left behind in case the process fails, you need to use rename and thus have to use a temporary location on the same filesystem. Often this is done by using different name in the same directory instead of using temporary directory.
  • If you are renaming it because the destination might be slow, e.g. because you want to put it on a network share, you obviously want to use temporary directory on different filesystem. That means you have to read the file and write it under the new name. There is no function for this in the standard C or C++ library, but many libraries will have one (and high-level languages like python will have one and you can always just execute /bin/mv to do it for you).

这篇关于将 SQLite 数据库复制到另一个路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆