Using SQLite with C#

Overview

Adding a database to your application can be an easy way to store data and settings between sessions for your program, but it is not always feasible to use a server based DBMS to store your database. SQLite is a small, fast, and reliable database which can be used without the end user having to install anything extra (achieved by referencing a single .dll in your project). There are a few things we as developers must do to get started with SQLite:

Add a reference to System.Data.SQLite to your project (and mark the .dll to be copied locally to your project)

Optionally Download a SQLite GUI Client and use it to design your DB (Feel free to code it by hand if that is your preference)

If the above section made sense to you, feel free to jump down to the section titled “Interacting with your Database”, otherwise keep reading!

Getting Started

Referencing System.Data.SQLite
After you have installed the .NET provider for SQLite, you need to make sure that your project can access the required .dll. In Visual Studio 2008, this can be done by selecting “Project -> Add Reference…” from the main menu bar. A window will pop up, and under the “.NET” tab, scroll down and find System.Data.SQLite.

Adding a Reference in Visual Studio

Select it and click ok. It is now referenced in your project. The last thing we need to do is make sure Visual Studio copies the .dll for System.Data.SQLite to the project folder, which is necessary for SQLite to work without the provider.

Viewing References in Visual Studio

If the Solution Explorer window is not currently visible, open it by selecting “View -> Solution Explorer” from the main menu bar. Under the current project, click the + sign next to References to see a list of all currently referenced libraries.

Right click the reference to System.Data.SQLite, and select “Properties”. Set the property “Copy Local” to true.

You have now successfully referenced SQLite, and it can be added to any file by “using System.Data.SQLite;”.

Using the SQLite GUI Client

SQLite Administrator is a very straightforward Client, and I am not going to go into much detail with its use. I will however note a few things that were not immediately evident to me when I first used it.

SQLite does not currently support foreign key constraints. Therefore SQLite Administrator does not have any way of linking tables via Foreign Key. That is certainly something to keep in mind.

The box on the left hand side is for viewing the current Database and all of it’s objects. If you see something you don’t want to see, or don’t see something you want to see, the buttons at the top of the box are toggle switches for tables, views, triggers, indexes, and so on. Since there are no tooltips, you’ll just have to play around to figure out which is which function.

Interacting with your Database

Once the database is set up, it is time to begin reading from it and writing to it. In order to facilitate the interaction with the DB, I have written a helper class. It should be noted that a portion of this code is adapted from sample code in this tutorial by Mike Duncan. The Methods GetDataTable(), ExecuteNonQuery(), and ExecuteScalar() are his code and not mine.

woot, thank you! I’ve finally came across a website where the owner knows what they’re talking about. You know how many results are in Google when I check.. too many! It’s so annoying having to go from page after page after page, wasting my day away with tons of owners just copying eachother’s articles… ugh. Anyway, thankyou for the information anyway, much appreciated.

Brendon Dugan

Brendon Dugan

Ok, it turns out that the System.data.SQLite project has been taken over by the SQLite team, and has now moved. I have updated my post to reflect the new location, and there is a 64 bit installer available. Thanks for pointing that out to me!

wontsay

JAD

I really enjoyed your blog, it helped me to get a better understanding of sqllite, I followed your sample code and worked like a charm when I used one table and just one row, then added another table with one row and join them together but I got the error below. I dropped all indexes, columns accept null values; however all of them are populated and no constraints. Any ideas?

“Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key”.

Brendon Dugan

Hey JAD, without seeing your code it is hard to tell, but looking at the error makes me wonder whether or not you are attempting to declare foreign key relationships. Last time I checked (admittedly, this was a while ago) SQLite didn’t support foreign keys, so that could be a portion of your problem.

Are you familiar with stackoverflow.com? You should post your question (with sample code) there, and then comment back with a link. I’ll check it out and hopefully be able to help you find a solution!

Hey rjl, thanks for the heads up on the Mike Duncan link! I am hoping that he just forgot to renew his domain name, so I’ll leave the link since it is a great resource. As for the recipes.s3db, I do not have it posted anywhere, but I will see if I can find the source code for that project and post it. Have a great day and thanks for stopping by!