2

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.

Tim Lehner
  • 14,813
  • 4
  • 59
  • 76
Sandy
  • 1,043
  • 2
  • 21
  • 32
  • Ummm...a SQLite database is a file. If you want to move the file, just move the file. There's no need to move a SQLite database from within said database. –  Sep 05 '12 at 06:48
  • I am moving the sqlite database file to another folder. – Sandy Sep 05 '12 at 06:51
  • Then move it. It's that simple. Why would you want a SQLite command to perform a file system function? –  Sep 05 '12 at 06:52
  • 3
    [You've already been told to just move the file.](http://stackoverflow.com/questions/12247363/rename-file-return-1-with-errno-18) Basic file system commands are nothing to be afraid of. –  Sep 05 '12 at 06:54
  • Is there any cpp function to move the file? – Sandy Sep 05 '12 at 07:01
  • How about [`MoveFile()`](http://msdn.microsoft.com/en-us/library/windows/desktop/aa365239(v=vs.85).aspx)? It took me about 15 seconds to find that link. Google is your friend. Use it. –  Sep 05 '12 at 07:03
  • MoveFile() is available only in Windows. What about Linux? – Sandy Sep 05 '12 at 07:12
  • [This link](http://www.linuxquestions.org/questions/programming-9/moving-file-using-c-in-linux-846358/) gives a couple of alternatives, but if you don't care about platform independence, there's always `system()`. –  Sep 05 '12 at 07:14

2 Answers2

4

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).
Jan Hudec
  • 73,652
  • 13
  • 125
  • 172
  • I am creating sqlite database file in the temp directory only. I want to move that sqlite file to another folder. I cannot use use rename() function, it returns error. – Sandy Sep 05 '12 at 06:54
  • @Sandy - Then **use the file system to move the file**. Don't fear the shell. –  Sep 05 '12 at 06:55
  • I will create the sqlite file in the same directory by a different name then use rename(). This will solve my problem. Thanks for your reply. – Sandy Sep 05 '12 at 07:22
  • Is there any c++ function to create a copy of a file? – Sandy Sep 06 '12 at 13:49
  • 1
    @Sandy: Standard library does not have any. There is one in [Boost.Filesystem](http://www.boost.org/libs/filesystem/doc/index.htm) and many other application frameworks will have one too (e.g. QtCore). But it's quite trivial to write one. Just open the two files, create a local fixed-size buffer and read from one + write to the other while there's anything to read. – Jan Hudec Sep 07 '12 at 06:49
0

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.

jww
  • 97,681
  • 90
  • 411
  • 885