Backup/Restore SQLite file

Castlemaine

Member
Licensed User
Longtime User
I have a database file stored on my SD card and I want to be able to back it up and restore it.

The file appears to copy just fine:

B4X:
File.Copy(File.DirDefaultExternal, "mydb.s3db", File.DirDefaultExternal, "mydb.bak")

So my database is now backed up.

I delete a record by mistake and go to restore the database.
Again, the file appears to copy just fine:

B4X:
File.Copy(File.DirDefaultExternal, "mydb.bak", File.DirDefaultExternal, "mydb.s3db")

I return to my screen happy in the knowledge that my record will have reappeared. Except that it doesn't.

:BangHead:

Here is my code:

B4X:
Sub Process_Globals
   
   'Database
   Dim SQL1 As SQL
   Dim DBFileDir As String         : DBFileDir = File.DirDefaultExternal
   Dim DBFileName As String      : DBFileName = "mydb.s3db"
   Dim DBFileBak As String         : DBFileBak = "mydb.bak"

End Sub

Sub BackupButton_Click
   
   SQL1.Close
   File.Copy(DBFileDir, DBFileName, DBFileDir, DBFileBak)
   SQL1.Initialize(DBFileDir, DBFileName, True)
   Msgbox("Database has been Backed Up", "")
   
End Sub

Sub RestoreButton_Click
   
   SQL1.Close
   File.Copy(DBFileDir, DBFileBak, DBFileDir, DBFileName)
   SQL1.Initialize(DBFileDir, DBFileName, True)
   Msgbox("Database has been Restored", "")

End Sub

Everything I've read so far says it should work. So where am I going wrong. I run the program on a HTC Desire HD. Please help :sign0085:
 

Castlemaine

Member
Licensed User
Longtime User
Thanks Klaus.

This code is just the backup/restore activity. Once it returns to the main program, the data is redisplayed but the data from the copied backup is not there. For example:

I have the details for John Smith and Jack Frost. The database is backed up. I accidentally delete John Smith so restore the database from the backup.

In my main screen, the details for Jack Frost come up, but not John Smith, even though the database has been queried and all the data redisplayed.

:sign0080:
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Sorry for beeing late.

Unfortunately the project you sent doesn't work.
- There is no PackageName nor an Application Label in the 'Allied Rota.b4a' file. This shows that you have never compiled the project with this file.
- There is an End Sub line missing at the end of the 'Allied Rota.b4a' file.
- There are ProcessGlobal variables missing in the Main module.

To be able to help you, you should post a working project.

Best regards.
 
Upvote 0

Castlemaine

Member
Licensed User
Longtime User
Apologies again.
I used the 'Export As Zip' option in the IDE and I have been compiling the program to my phone for some time. Don't know what went wrong. Anyway. I have attached an updated version. Hope this works for you.:sign0148:

Thanks Klaus.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Here you are, I finaly got it, took me a while.
You declare SQL1 and the database names in all Prodess_Globals routines in the Activity modules where you use the database.
You should declare Prodess_Global variables only ONCE in the project.
Attached the modified version.

Best regards.
 

Attachments

  • RotaNew.zip
    20.2 KB · Views: 567
Upvote 0
Top