Sandy
Sandy

Reputation: 1053

Copy SQLite database to another path

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.

Upvotes: 2

Views: 9940

Answers (2)

jww
jww

Reputation: 102326

Now I want to copy that file to another folder. Is there any sqlite command to rename the sqlite database file?

Close the database. Copy the database to the new path using the shell.

Also see Distinctive Features Of SQLite:

Stable Cross-Platform Database File

The SQLite file format is cross-platform. A database file written on one machine can be copied to and used on a different machine with a different architecture. Big-endian or little-endian, 32-bit or 64-bit does not matter. All machines use the same file format. Furthermore, the developers have pledged to keep the file format stable and backwards compatible, so newer versions of SQLite can read and write older database files.

Most other SQL database engines require you to dump and restore the database when moving from one platform to another and often when upgrading to a newer version of the software.

Upvotes: 0

Jan Hudec
Jan Hudec

Reputation: 76306

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).

Upvotes: 4

Related Questions