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:
- Install the .NET provider for SQLite from Sourceforge.net
- 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.
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. 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.
Using the SQLiteDatabase Helper Class SQLiteDatabase.cs
002 | using System.Collections.Generic; |
004 | using System.Data.SQLite; |
005 | using System.Windows.Forms; |
012 | /// Default Constructor for SQLiteDatabase Class. |
014 | public SQLiteDatabase() |
016 | dbConnection = "Data Source=recipes.s3db" ; |
020 | /// Single Param Constructor for specifying the DB file. |
022 | /// <param name="inputFile">The File containing the DB</param> |
023 | public SQLiteDatabase(String inputFile) |
025 | dbConnection = String.Format( "Data Source={0}" , inputFile); |
029 | /// Single Param Constructor for specifying advanced connection options. |
031 | /// <param name="connectionOpts">A dictionary containing all desired options and their values</param> |
032 | public SQLiteDatabase(Dictionary<String, String> connectionOpts) |
035 | foreach (KeyValuePair<String, String> row in connectionOpts) |
037 | str += String.Format( "{0}={1}; " , row.Key, row.Value); |
039 | str = str.Trim().Substring(0, str.Length - 1); |
044 | /// Allows the programmer to run a query against the Database. |
046 | /// <param name="sql">The SQL to run</param> |
047 | /// <returns>A DataTable containing the result set.</returns> |
048 | public DataTable GetDataTable( string sql) |
050 | DataTable dt = new DataTable(); |
053 | SQLiteConnection cnn = new SQLiteConnection(dbConnection); |
055 | SQLiteCommand mycommand = new SQLiteCommand(cnn); |
056 | mycommand.CommandText = sql; |
057 | SQLiteDataReader reader = mycommand.ExecuteReader(); |
064 | throw new Exception(e.Message); |
070 | /// Allows the programmer to interact with the database for purposes other than a query. |
072 | /// <param name="sql">The SQL to be run.</param> |
073 | /// <returns>An Integer containing the number of rows updated.</returns> |
074 | public int ExecuteNonQuery( string sql) |
076 | SQLiteConnection cnn = new SQLiteConnection(dbConnection); |
078 | SQLiteCommand mycommand = new SQLiteCommand(cnn); |
079 | mycommand.CommandText = sql; |
080 | int rowsUpdated = mycommand.ExecuteNonQuery(); |
086 | /// Allows the programmer to retrieve single items from the DB. |
088 | /// <param name="sql">The query to run.</param> |
089 | /// <returns>A string.</returns> |
090 | public string ExecuteScalar( string sql) |
092 | SQLiteConnection cnn = new SQLiteConnection(dbConnection); |
094 | SQLiteCommand mycommand = new SQLiteCommand(cnn); |
095 | mycommand.CommandText = sql; |
096 | object value = mycommand.ExecuteScalar(); |
100 | return value.ToString(); |
106 | /// Allows the programmer to easily update rows in the DB. |
108 | /// <param name="tableName">The table to update.</param> |
109 | /// <param name="data">A dictionary containing Column names and their new values.</param> |
110 | /// <param name="where">The where clause for the update statement.</param> |
111 | /// <returns>A boolean true or false to signify success or failure.</returns> |
112 | public bool Update(String tableName, Dictionary<String, String> data, String where) |
115 | Boolean returnCode = true ; |
118 | foreach (KeyValuePair<String, String> val in data) |
120 | vals += String.Format( " {0} = '{1}'," , val.Key.ToString(), val.Value.ToString()); |
122 | vals = vals.Substring(0, vals.Length - 1); |
126 | this .ExecuteNonQuery(String.Format( "update {0} set {1} where {2};" , tableName, vals, where)); |
136 | /// Allows the programmer to easily delete rows from the DB. |
138 | /// <param name="tableName">The table from which to delete.</param> |
139 | /// <param name="where">The where clause for the delete.</param> |
140 | /// <returns>A boolean true or false to signify success or failure.</returns> |
141 | public bool Delete(String tableName, String where) |
143 | Boolean returnCode = true ; |
146 | this .ExecuteNonQuery(String.Format( "delete from {0} where {1};" , tableName, where)); |
148 | catch (Exception fail) |
150 | MessageBox.Show(fail.Message); |
157 | /// Allows the programmer to easily insert into the DB |
159 | /// <param name="tableName">The table into which we insert the data.</param> |
160 | /// <param name="data">A dictionary containing the column names and data for the insert.</param> |
161 | /// <returns>A boolean true or false to signify success or failure.</returns> |
162 | public bool Insert(String tableName, Dictionary<String, String> data) |
166 | Boolean returnCode = true ; |
167 | foreach (KeyValuePair<String, String> val in data) |
169 | columns += String.Format( " {0}," , val.Key.ToString()); |
170 | values += String.Format( " '{0}'," , val.Value); |
172 | columns = columns.Substring(0, columns.Length - 1); |
173 | values = values.Substring(0, values.Length - 1); |
176 | this .ExecuteNonQuery(String.Format( "insert into {0}({1}) values({2});" , tableName, columns, values)); |
178 | catch (Exception fail) |
180 | MessageBox.Show(fail.Message); |
187 | /// Allows the programmer to easily delete all data from the DB. |
189 | /// <returns>A boolean true or false to signify success or failure.</returns> |
190 | public bool ClearDB() |
195 | tables = this .GetDataTable( "select NAME from SQLITE_MASTER where type='table' order by NAME;" ); |
196 | foreach (DataRow table in tables.Rows) |
198 | this .ClearTable(table[ "NAME" ].ToString()); |
209 | /// Allows the user to easily clear all data from a specific table. |
211 | /// <param name="table">The name of the table to clear.</param> |
212 | /// <returns>A boolean true or false to signify success or failure.</returns> |
213 | public bool ClearTable(String table) |
218 | this .ExecuteNonQuery(String.Format( "delete from {0};" , table)); |
Usage: Query: 03 | db = new SQLiteDatabase(); |
05 | String query = "select NAME \"Name\", DESCRIPTION \"Description\"," ; |
06 | query += "PREP_TIME \"Prep Time\", COOKING_TIME \"Cooking Time\"" ; |
07 | query += "from RECIPE;" ; |
08 | recipe = db.GetDataTable(query); |
10 | recipeDataGrid.DataSource = recipe; |
25 | String error = "The following error has occurred:\n\n" ; |
26 | error += fail.Message.ToString() + "\n\n" ; |
27 | MessageBox.Show(error); |
Insert: 01 | db = new SQLiteDatabase(); |
02 | Dictionary<String, String> data = new Dictionary<String, String>(); |
03 | data.Add( "NAME" , nameTextBox.Text); |
04 | data.Add( "DESCRIPTION" , descriptionTextBox.Text); |
05 | data.Add( "PREP_TIME" , prepTimeTextBox.Text); |
06 | data.Add( "COOKING_TIME" , cookingTimeTextBox.Text); |
07 | data.Add( "COOKING_DIRECTIONS" , "Placeholder" ); |
10 | db.Insert( "RECIPE" , data); |
14 | MessageBox.Show(crap.Message); |
Update: 01 | db = new SQLiteDatabase(); |
02 | Dictionary<String, String> data = new Dictionary<String, String>(); |
04 | data.Add( "NAME" , nameTextBox.Text); |
05 | data.Add( "DESCRIPTION" , descriptionTextBox.Text); |
06 | data.Add( "PREP_TIME" , prepTimeTextBox.Text); |
07 | data.Add( "COOKING_TIME" , cookingTimeTextBox.Text); |
10 | db.Update( "RECIPE" , data, String.Format( "RECIPE.ID = {0}" , this .RecipeID)); |
14 | MessageBox.Show(crap.Message); |
Delete: 1 | db = new SQLiteDatabase(); |
3 | db.Delete( "RECIPE" , String.Format( "ID = {0}" , recipeID)); |