如果SQLite數據庫不需要在分發之前用太多的數據填充,您可以在運行時創建它。如果你希望它是在同一位置的可執行文件,試試這個來創建它(VB.Net代碼)
Dim SQLconnect As New SQLite.SQLiteConnection()
SQLconnect.ConnectionString = "Data Source=" & Application.StartupPath & "\database.db;"
SQLconnect.Open()
SQLconnect.Close()
這將創建一個空數據庫文件,如果你想創建表和/或加載一些數據,你可以做這樣的事情(檢查,看是否表已經存在,當然後):
If tblSettingsExist = False Then
SQLconnect.Open()
SQLcommand = SQLconnect.CreateCommand
SQLcommand.CommandText = "CREATE TABLE Settings(setting TEXT PRIMARY KEY, value TEXT);"
SQLcommand.ExecuteNonQuery()
SQLcommand.Dispose()
SQLconnect.Close()
'add records for all default settings
SQLite_InsertRecord("Settings", "setting, value", "'someSetting1', '-1'")
SQLite_InsertRecord("Settings", "setting, value", "'someSetting2', '0'")
SQLite_InsertRecord("Settings", "setting, value", "'someSetting3', '1'")
End If
只是爲了完整性,這裏是我使用插入數據子程序:
Public Sub SQLite_InsertRecord(ByVal table As String, ByVal fields As String, ByVal values As String)
Dim SQLcommand As SQLiteCommand
SQLconnect.Open()
SQLcommand = SQLconnect.CreateCommand
SQLcommand.CommandText = "INSERT INTO " & table & " (" & fields & ") VALUES (" & values & ")"
Try
SQLcommand.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Database Insertion Error")
Finally
SQLcommand.Dispose()
SQLconnect.Close()
End Try
End Sub
我在關於在這裏或在Code Review上詢問這個問題,但我認爲我會先在這裏嘗試。 – Mattio 2011-04-09 02:52:31