0

I have a small application I'm working on and part of the project's goals is simply to get more familiar with SQLite. The main entry point is a console app (sub-project named Console), but the data access code is in a different sub-project (named Infrastructure). I just created a folder in Infrastructure named Data and dropped the .sqlite file there and added some dummy tables and data.

The project looks like this:

  • Solution
    • Console (project)
    • Domain (project)
    • Infrastructure (project)
      • Data (folder)

My original connection string just looked like this:

var cnx = new SQLiteConnection("Data Source=testing.sqlite; FailIfMissing=True");

What I first saw happening was that a 0KB testing.sqlite was being dropped in the Debug directory. A simple count query would fail because the table I was referencing obviously didn't exist in the empty file. After some playing around, I found that if I set the properties on the .sqlite file to be Content and Copy Always and if I changed the connection string, I could connect as expected.

var cnx = new SQLiteConnection("Data Source=Data\\testing.sqlite; FailIfMissing=True");

Am I missing something here, or is this indeed how I should be including the file in my project?

1
  • I was on the fence about asking this here or on Code Review, but thought I'd try here first.
    – Mattio
    Apr 9, 2011 at 2:52

2 Answers 2

1

I'm thinking the best way to go here is probably to move the file outside of the project and just use a setting in App.config.

In App.config:

<connectionStrings>
  <add name="mydb" connectionString="Data Source=D:\\Data\\testing.sqlite; FailIfMissing=True"/>
</connectionStrings>

In code:

private string _connectionString = ConfigurationManager.ConnectionStrings["mydb"].ConnectionString;

If I want the physical file in my project I can continue to do what I noted above.

0

If the SQLite database does not need to be populated with much data before distribution, you can just create it on runtime. If you want it to be in the same location as your executable, try this to create it (VB.Net code)

 Dim SQLconnect As New SQLite.SQLiteConnection()
 SQLconnect.ConnectionString = "Data Source=" & Application.StartupPath & "\database.db;"
 SQLconnect.Open()
 SQLconnect.Close()

This will create a blank database file and if you want to create the tables and/or load some data, you could do something like this (after checking to see if the tables already exist of course):

    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

Just for completeness, here is the subroutine I use to insert data:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.