Skip to content

Backwards SQLite Backups

If you use SQLite databases in your apps you may or may not be aware of the Backup method which allows you to quickly and asynchronously create a backup of an existing connected database. This is especially great if you have an in-memory database and you want to store that data on disk for later reference. I am using the simpler synchronous calls for this example:

memoryDB.Backup(fileDB, Nil, -1) 

Something that’s often overlooked is that you can use the Backup method to go the other way… that is, it can be used to load a disk-based database back into memory by simply swapping the parameters:

// Connect to the database file on disk
Var fileDB as New SQLiteDatabase
fileDB.DatabaseFile = SpecialFolder.Resources.Child("template.db")
fileDB.Connect

// Create the database in memory
Var memoryDB as New SQLiteDatabase
memoryDB.Connect

// "Backup" the file database into memory
fileDB.Backup(memoryDB, Nil, -1)

// Close the file database
fileDB.Close

And voila! You’ve restored the database back into memory!

For more information about this command, please see SQLiteDatabase.Backup on https://docs.xojo.com.