Tutorials Books Videos Forums

Change the theme! Search!
Rambo ftw!

Customize Theme


Color

Background


Done

SQLite in VB.net

by Austin Andrews aka Templarian   | filed under .NET and C#

This is an archived tutorial from the kirupa.com legacy collection. It covers software that may no longer be available, but it is kept online because the ideas still hold up.

SQLite is a very flexible relational database that allows users to store rows of data for their programs. Because it is highly used, database format wrappers are found in almost all languages for it. This tutorial will focus on the basics of installing and using the most popular Visual Basic .NET wrapper System.Data.SQLite - created and maintained by Robert Simpson. If you are familiar with ADO.NET you will find this very similar. Don't worry if you aren't though, for it it is quite simple to use.

Installation of System.Data.SQLite

For this step you will need any version of Microsoft Visual Studio 2005/2008. The tutorial is written with Visual Studio 2008 in mind, but all steps are the same on either version.
Please follow the link below to download System.Data.SQLite off the official site:
http://sqlite.phxsoftware.com/

Note

Make sure to download the latest non-Alpha version, and you will want to download the “SQLite-X.X.XX.X-setup.exe” as this is the quickest method of installation.

Click next through the installation wizard till you get to this screen:

For those using VS 2008 Express Design-Time support does not work, so do not install it. This is not a glitch, it is disabled by Microsoft in the Express edition of 2008, but does work in the 2005 Express Edition. This is not used in the tutorial so it will not matter either way if you have it installed it.

Installing the SQLite Design-Time Support is optional for those that need to test queries. It is rather easy to use and understand, and I’m sure you won’t need it after this tutorial or if you know the basics of SQL already.

Create a VB.NET New Project

Let’s start up Visual Studio 2005/2008 and create a New Visual Basic » Windows Form Application. I created this with .NET 2.0 as it is the lowest version of .NET that is supported.

Creating a Database

First understand that a database is a completely empty file when it’s first created, and if the database isn’t found where you open the connection it will create a new file.
First create a button and name it btn_createdb and double click the button to bring up the code viewer.

The code that you will be placing into the button basically opens up the Windows save dialog so that you can simply save your database where you want:

When saving a database, the standard extension is .db3, but if you are using this for back up purposes, you may choose your own extension.

Remember that in a real example, you will usually never create an empty database.

Creating a Table

The first thing to understand is your basic data types that you have to work with. SQLite, being a very compact database, has 4 main (not counting NULL) such data types. This makes it really simple to know which type your data should be in.

(Source: http://www.sqlite.org/datatype3.html)

What you will do is add some code to create a table. Before delving into the code, let's take a look at what your table will look like first:

Now that you know what we will be creating, let's add it. First start by creating a button called btn_createtable. Double click the button to be taken to the code view where you will be inside this button's event handler.

Copy and paste the following code:

You created a table called foo, and if you were to visualize it, it looks just like the image of the table I showed you earlier. Ok, let's take a small break and continue in the next section.

In the previous section, we started looking at some simple database operations you can do using SQLite. In this page, let's continue doing more of the same.

Inserting a Record (Update/Delete)

Just like in all SQL, if you know how to insert a record, you can change the SQL query to update and delete a record. Create another button called btn_insert and add the fallowing code:

Reading Records into a List

What’s the use of putting all this data into a database if you cannot show it off? First create a button called “btn_readrecord”, and a listbox called “lst_records”, place the code below into the button:

Reading Tables into a Drop Down

Most projects require multiple tables to store all the relational information. And most of the times the tables are used to organize categories, so a way to find these tables is needed as shown with the code following code example.

But, first, create a button called btn_tables, and a Combo Box called cmb_tables. Once you have done that, then copy and paste the following code:

Writing/Reading a BLOB Image into the Table

I have left actually writing and reading an image out of the examples thus far because it’s optional and easier to explain out of context. With that said, it is hard enough that one would be mad if I left it out. If you are interested to see this included into the other source, it is a highly recommended to download the source example I've provided at the end of this tutorial that is based on the sample code you see here.

For this, simply create a button called btn_insertimage, a button called btn_updateimage, a button called btn_viewimage, and a picture box called pic_viewer. This example creates a database with a table called foo with 2 columns and image BLOB. In the table, one record is inserted and viewable. The image is always entered into where id is equal to 1.

The code is:

Summary

Hopefully with all these code examples and short explanations, you should be better prepared for your future projects that use SQLite.

Download the source for this project. The source example is a fully working database editor for a simple database with an id, title, description, and image fields:

All of which are stored in the databases. It is highly recommended to read through the source of the example. If you have any questions, please post on the forums.

Austin Andrews aka Templarian

Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence, ads, and algorithm-driven doodads. A huge thank you to all of you who buy kirupa's books, became a paid subscriber, watch the videos, and/or interact on the forums.

Your support keeps this site going! 😇

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright KIRUPA 2026 //--