0% found this document useful (0 votes)
76 views

Sqlite Database Backup Restore

This code defines methods to backup and restore a SQLite database. The BackupDatabase method copies the database structure and all data from the source database to the destination database. It gets a list of all tables, drops and recreates each table in the destination, and inserts all rows. The RestoreDatabase method simply copies the source file to the destination path. Buttons call the backup and restore methods when clicked.

Uploaded by

niktheonedroid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views

Sqlite Database Backup Restore

This code defines methods to backup and restore a SQLite database. The BackupDatabase method copies the database structure and all data from the source database to the destination database. It gets a list of all tables, drops and recreates each table in the destination, and inserts all rows. The RestoreDatabase method simply copies the source file to the destination path. Buttons call the backup and restore methods when clicked.

Uploaded by

niktheonedroid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Imports System.Data.

SQLite

Public Class Form1

Private Sub BackupDatabase(ByVal sourceFile As String, ByVal destinationFile As String)

Try

Using sourceConn As New SQLiteConnection($"Data Source={sourceFile};Version=3;"),

destConn As New SQLiteConnection($"Data Source={destinationFile};Version=3;")

sourceConn.Open()

destConn.Open()

Using sourceCmd As SQLiteCommand = sourceConn.CreateCommand(),

destCmd As SQLiteCommand = destConn.CreateCommand()

sourceCmd.CommandText = "BEGIN IMMEDIATE; " & "SELECT * FROM sqlite_master


WHERE type='table';"

destCmd.CommandText = "PRAGMA foreign_keys = OFF;"

sourceCmd.ExecuteNonQuery()

destCmd.ExecuteNonQuery()

Dim reader As SQLiteDataReader = sourceCmd.ExecuteReader()

Dim totalTables As Integer = 0

While reader.Read()

Dim tableName As String = reader.GetString(1)

If tableName = "sqlite_sequence" Then Continue While

totalTables += 1

End While

reader.Close()
reader = sourceCmd.ExecuteReader()

Dim currentTable As Integer = 0

While reader.Read()

Dim tableName As String = reader.GetString(1)

If tableName = "sqlite_sequence" Then Continue While

destCmd.CommandText = $"DROP TABLE IF EXISTS [{tableName}];"

destCmd.ExecuteNonQuery()

Dim createTableSql As String = reader.GetString(4)

destCmd.CommandText = createTableSql

destCmd.ExecuteNonQuery()

destCmd.CommandText = $"INSERT INTO [{tableName}] SELECT * FROM


[{tableName}]"

destCmd.ExecuteNonQuery()

currentTable += 1

UpdateProgress(currentTable, totalTables)

End While

reader.Close()

destCmd.CommandText = "PRAGMA foreign_keys = ON;"

destCmd.ExecuteNonQuery()

End Using

End Using

MessageBox.Show("Backup completed.")

Catch ex As Exception

MessageBox.Show("Error occurred during backup: " & ex.Message)


End Try

End Sub

Private Sub RestoreDatabase(ByVal sourceFile As String, ByVal destinationFile As String)

Try

FileCopy(sourceFile, destinationFile)

MessageBox.Show("Restore completed.")

Catch ex As Exception

MessageBox.Show("Error occurred during restore: " & ex.Message)

End Try

End Sub

Private Sub UpdateProgress(ByVal currentProgress As Integer, ByVal totalProgress As Integer)

If totalProgress > 0 Then

Dim percentage As Integer = (currentProgress * 100) / totalProgress

ProgressBar1.Value = percentage

ProgressBar1.Update()

End If

End Sub

Private Sub BackupButton_Click(sender As Object, e As EventArgs) Handles BackupButton.Click

Dim sourceDB As String = "path/to/source.db"

Dim backupDB As String = "path/to/backup.db"

BackupDatabase(sourceDB, backupDB)

End Sub

Private Sub RestoreButton_Click(sender As Object, e As EventArgs) Handles RestoreButton.Click

Dim backupDB As String = "path/to/backup.db"

Dim sourceDB As String = "path/to/source.db"


RestoreDatabase(backupDB, sourceDB)

End Sub

End Class

You might also like