This article briefly discusses what project possibilities there are for SQLite, which type of project are suited and which are not. It walks you through a practical example with a demo application that uses SQLite database to store id and description value pairs in a single table – mains. We will display the contents of this table in a grid with added functionalities of adding/deleting and modifying records to and from the table.
SQLite is a small, fast and embeddable database where the database engine and the interface are combined into a single library. It also has the ability to store all the data in a single file. So if your application requires a standalone database, SQLite is perhaps the perfect choice for you. There are, of course, other reasons for choosing SQLite including:
- SQLite has a small memory footprint and only a single library is required to access databases, making it ideal for embedded database applications.
- SQLite has been ported to many platforms and runs even on Windows CE and Palm OS.
- SQLite is ACID-compliant, meeting all four criteria - Atomicity, Consistency, Isolation, and Durability.
- SQLite implements a large subset of the ANSI-92 SQL standard, including views, sub-queries and triggers.
- No problem of extra database drivers, ODBC configuration required. Just include the library and the data file with your application.
- SQLite has native language APIs for C/C++, PHP, Perl, Python, Tcl etc. Native API for C# is still not present.
Using the Code
To use SQLite in your C# application, you need to download a third party free ADO.NET data provider (SQLite.NET.0.21_x68_dll.zip) from here.
Fig 1: Demo C# SQLite application.
Our demo application uses SQLite database to store
description value pairs in a single table –
mains. We will display the contents of this table in a grid with added functionalities of adding/deleting and modifying records to and from the table. To set up the system, please follow the steps below:
Unzip the archive, and place the *.dll files into the Binary folder.
Use the SQLite.NET.dll in your .NET application by ‘Project -> Add Reference’.
Using Finisar.SQLite; in the
using directive declaration region of your *.cs file.
Declare the following
private fields to be used in your application:
private SQLiteConnection sql_con;
private SQLiteCommand sql_cmd;
private SQLiteDataAdapter DB;
private DataSet DS = new DataSet();
private DataTable DT = new DataTable();
Create a function to set up the Connection String. DemoT.db is the name of the single data file. It already has a single table –
mains. Version (=3) is the version of the database engine.
private void SetConnection()
sql_con = new SQLiteConnection
Create a generic function
ExecuteQuery, to execute Create Command queries.
private void ExecuteQuery(string txtQuery)
sql_cmd = sql_con.CreateCommand();
Now create a
LoadData function to access the SQLite database and retrieve the data from the
mains table and fill the Dataset. Please refer to the following code:
private void LoadData()
sql_cmd = sql_con.CreateCommand();
string CommandText = "select id, desc from mains";
DB = new SQLiteDataAdapter(CommandText,sql_con);
Grid.DataSource = DT;
To add/edit/delete an entry to and from the table, just pass the required query to the already created
ExecuteQuery function. Please see the following code for an example:
private void Add()
string txtSQLQuery = "insert into mains (desc) values ('"+txtDesc.Text+"')";
To administer SQLite database, an open-source SQLite GUI Database Browser utility is of much use. It can be downloaded from here.
So should SQLite be used for all database driven applications? No.
Like all databases, SQLite has its list of shortcomings. It is not suitable for a client server application or as a networked database. It’s not suited well for a multi user scenario and can have serious file locking issues when accessed simultaneously over the network. Quite for the same reason, SQLite is not suited for a multi-threaded or a multi-process application-database access scenario.
To conclude, SQLite is very suitable for memory constraint systems like WinCE, Palms, Smart Phones, embedded devices and also normal single user desktop applications where its small memory footprint, single library and its copy and paste deployment feature give it a distinct advantage.
- Visual Studio .NET 2003 (C#)
- SQLite (www.sqlite.org)
- 16-Dec-2007: Initial publication
Chayan Ray has been working as a Technical Consultant in a CMM level 5 company in India. His technical domain includes ASP.NET, C#, PHP, Perl, Cold Fusion, MySQL and MSSQL 2000.