1 (edited by v_pozidis 2022-03-12 22:54:55)

Topic: Restore

Hi, I have create a backup procedure by script for the database sqlite.db. But how can I make a restore procedure? As I know I can not restore the file when the database is active . Any idea???

2 (edited by Destiny 2022-03-13 08:25:05)

Re: Restore

Hello v_pozidis, we cannot restore a database that is in use. You need to close the application and copy "sqlite.db" from the backup and paste it in your program folder in order to replace the existing one.
Destiny

Destiny

3 (edited by k245 2022-03-14 05:29:10)

Re: Restore

v_pozidis wrote:

Hi, I have create a backup procedure by script for the database sqlite.db. But how can I make a restore procedure? As I know I can not restore the file when the database is active . Any idea???

Actions to restore the database (copying files) can be performed from a batch file. So the scenario is:

1. The program creates a batch file, runs it and exits
2. A small delay (1-2 seconds) is made in the batch file so that the program is guaranteed to close and release access to the database file.
3. In the batch file, the database file is copied.
4. The main program is launched in the batch file.

Done: The database has been restored from a backup.

Визуальное программирование: блог и телеграм-канал.

Re: Restore

I had already used a Batch file some time ago but I abandoned the idea because the database file is not replaceable in use of this one.

Destiny

Re: Restore

Thank you...I will try to create a script using Lazarus pascal to rename and copy the file from the backup folder to the application folder or does any one has already the script???

Re: Restore

Try this one.

Post's attachments

Attachment icon Backup.zip 550.68 kb, 206 downloads since 2022-03-14 

brian

Re: Restore

Your solution works, how does it manage to replace the sqlite in use.

Destiny

Re: Restore

Form1.close; // will also close the database

// replace database with the selected backup
CopyFile(OpenDialog.FileName, ExtractFilePath(Application.ExeName)+'\sqlite.db');

// re open the application
OpenFile('samplebackup.exe');

Just a hunch that closing the main(Form1 in the sample) form will also close the database connection, then after that, you can replace the sqlite.db by the backup. Then running the executable afterwards.

brian

Re: Restore

Thanks, the code is indeed working.

Destiny

Re: Restore

What I can not understand is that if the program runs the database is active. So with your example when you close the program Form1.close how does the function replace works???

11 (edited by Destiny 2022-03-14 18:15:05)

Re: Restore

In the code there is a part that saves the database and another part that closes the program, restores the database and re-opens the program, all of this is almost invisible.

Destiny

12 (edited by brian.zaballa 2022-03-15 00:05:00)

Re: Restore

v_pozidis wrote:

What I can not understand is that if the program runs the database is active. So with your example when you close the program Form1.close how does the function replace works???

You must first backup the database using the backup button. The button will backup your current sqlite.db to [filename].dbbak using SaveDialog


Then the Add record button is just to populate the table to see if populated table will be replaced by the backup.


To backup, you then click Backup button in which the program will ask the user for a file(backup, the one you save with Backup button) using the OpenDialog, then when the user select [filename].dbbak, it will close Form1, then replace sqlite.db with the user's selected [filename].dbbak, then reopen the program

brian

Re: Restore

I have understood your software script. The program works correct. My question is how is it possible when you close the program to change the database ...,'' it will close Form1, then replace sqlite.db with the user's selected [filename].dbbak, then reopen the program''
.... i will use your script. .. thanks

Re: Restore

v_pozidis wrote:

I have understood your software script. The program works correct. My question is how is it possible when you close the program to change the database ...,'' it will close Form1, then replace sqlite.db with the user's selected [filename].dbbak, then reopen the program''
.... i will use your script. .. thanks

There is an assumption that when the main form close event occurs, the connection to the database file is closed, and this happens before the application terminates. And judging by the above code, this assumption is correct.

Визуальное программирование: блог и телеграм-канал.