Robert Simpson:You, like so many before you (and many to come after!) have forgotten the cardinal rule of SQLite ... TRANSACTIONS!
Yes, when I first wrote my program that inserts data from an MS SQLServer DB to an SQLite DB, I knew about transactions but didn't understand how much they affected the speed of updates/inserts/deletions. Nor did I know how to manually start and commit them from my code. After reading this post http://sqlite.phxsoftware.com/forums/p/134/465.aspx#465, my code went from "Will this program never finish updating" to "I'm not going to have enough time to finish my coffee".
Here's the code I came up with for fast parameterized inserting:
Public Sub FastInsertMany(ByRef crrntTable As DataTable)
' Insert new data into the SQLite DB file.
' This insert code is based on the code found at http://sqlite.phxsoftware.com/forums/p/134/465.aspx#465
' See section called "Fastest universal way to insert data using standard ADO.NET constructs"
Dim cnnSQLite As SQLite.SQLiteConnection = New SQLite.SQLiteConnection(<SQLiteConnectionString>)
Dim ltTransaction As SQLite.SQLiteTransaction
Dim cmmSQLite As SQLite.SQLiteCommand = New SQLite.SQLiteCommand(cnnSQLite)
cmmSQLite.CommandType = CommandType.Text
cmmSQLite.CommandText = "INSERT INTO " & crrntTable.TableName & Me.getInsertColumns(crrntTable.Columns)
For Each crrntColumn As DataColumn In crrntTable.Columns
cmmSQLite.Parameters.AddWithValue("@" & crrntColumn.ColumnName, crrntTable.Rows(0).Item(crrntColumn.ColumnName))
Next
cnnSQLite.Open()
ltTransaction = cnnSQLite.BeginTransaction
For Each crrntRow As DataRow In crrntTable.Rows
For Each crrntColumn As DataColumn In crrntTable.Columns
cmmSQLite.Parameters.Item("@" & crrntColumn.ColumnName).Value = crrntRow.Item(crrntColumn.ColumnName)
Next
cmmSQLite.ExecuteNonQuery()
Next
ltTransaction.Commit()
cnnSQLite.Close()
End Sub
Private Function getInsertColumns(ByRef crrntColumns As DataColumnCollection) As String
' build the columns and parameter variables used in the SQLlite insert command
getInsertColumns = " (,) VALUES (;)"
For Each crrntColumn As DataColumn In crrntColumns
getInsertColumns = getInsertColumns.Insert(getInsertColumns.LastIndexOf(","c), crrntColumn.ColumnName & ",")
getInsertColumns = getInsertColumns.Insert(getInsertColumns.LastIndexOf(";"c), "@" & crrntColumn.ColumnName & ";")
Next
getInsertColumns = getInsertColumns.Remove(getInsertColumns.LastIndexOf(","c) - 1, 2)
getInsertColumns = getInsertColumns.Remove(getInsertColumns.LastIndexOf(";"c) - 1, 2).Replace(";"c, ","c)
End Function