Backing up SQLite database from a program lazarus / 16-Databases ---------------------------------------------------------------------- JanRoza · 2010-07-09 20:42 Has anyone an example of how to make a backup of a SQLite database from a program that has the database opened? A simple CopyFile doesn't work as the active program apparently has a lock on the file. I saw some examples on the SQLite website but they are in C which I'm not too familiar with. Thanks! Jan ---------------------------------------------------------------------- theo · 2010-07-09 21:02 If you have a look at CopyFile implementation, you see that the SrcFS is created with fmShareDenyWrite. This is probably to prevent that the file is being written while you copy it. http://www.freepascal.org/docs-html/rtl/sysutils/fmsharedenywrite.html You may try to copy this code using http://www.freepascal.org/docs-html/rtl/sysutils/fmsharedenynone.html Good luck! ---------------------------------------------------------------------- JanRoza · 2010-07-11 16:47 That indeed was the solution. I used the following code and that worked like clockwork: Code: [Select]Procedure TfrmMain.CopyFile(Source: String; Dest: string); var   SourceStream, DestStream: Classes.TFileStream; // source and dest file streams Begin   DestStream := nil;   // Open source and dest file streams   SourceStream := Classes.TFileStream.Create(Source, SysUtils.fmOpenRead or SysUtils.fmShareDenyNone);   // original used SysUtils.fmShareDenyWrite   try     DestStream := Classes.TFileStream.Create(Dest, Classes.fmCreate or SysUtils.fmShareExclusive);     // Copy file from source to dest     DestStream.CopyFrom(SourceStream, SourceStream.Size);     // Set dest file's modification date to same as source file     SysUtils.FileSetDate(DestStream.Handle, SysUtils.FileGetDate(SourceStream.Handle));   finally     // Close files     DestStream.Free;     SourceStream.Free;   end; End; So one more SQLite riddle solved, I'm beginning to like SQLite more and more