Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sqlite Online Backup Using System.Data.Sqlite

Tags:

How can a sqlite database be backed up in native C# code while the database is still online? All of the online backup api examples are in C code.

like image 842
Elias Avatar asked May 08 '15 17:05

Elias


People also ask

How you can take the backup of SQLite database?

Historically, backups (copies) of SQLite databases have been created using the following method: Establish a shared lock on the database file using the SQLite API (i.e. the shell tool). Copy the database file using an external tool (for example the unix 'cp' utility or the DOS 'copy' command).

Can you host a SQLite database online?

SQLite can still be made to work in many remote database situations, but a client/server solution will usually work better in that scenario.


1 Answers

The online backup API was added to System.Data.Sqlite in version 1.0.80.0 - April 1, 2012. You can create a database backup while there are other external connections like so

using(var source = new SQLiteConnection("Data Source=ActiveDb.db; Version=3;")) using(var destination = new SQLiteConnection("Data Source=BackupDb.db; Version=3;")) {     source.Open();     destination.Open();     source.BackupDatabase(destination, "main", "main", -1, null, 0); } 

Also, BackupDb.db will be created if it doesn't already exist.

like image 154
Elias Avatar answered Nov 11 '22 21:11

Elias