Skip to main content

SQLite Database

Docs under migration

These docs are under active migration and updates. The original docs will remain available at docs.ghielectronics.com for future reference.

According to the SQLite homepage, "SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. SQLite is the most widely deployed SQL database engine in the world." SQLite lets you set up a database that resides entirely in a single file on a persistent storage device.

tip

Needed NuGets: GHIElectronics.TinyCLR.Data.SQLite

The code below is a simple example that creates a database file in RAM (SD cards and USB drives can be used as well). A table is created, a few rows are filled, and then this data is read from the database. This data is then iterated over and printed out. ColumnNames returns the name of each of the columns.

using System.Collections;
using System.Diagnostics;
using GHIElectronics.TinyCLR.Data.SQLite;

using (var db = new SQLiteDatabase()) {

Debug.WriteLine("Executing 1...");
db.ExecuteNonQuery("CREATE TABLE Test (Var1 TEXT, Var2 INTEGER, Var3 DOUBLE);");

Debug.WriteLine("Executing 2...");
db.ExecuteNonQuery("INSERT INTO Test(Var1, Var2, Var3) VALUES ('Hello, World!', 25, 3.14);");

Debug.WriteLine("Executing 3...");
db.ExecuteNonQuery("INSERT INTO Test(Var1, Var2, Var3) VALUES('Goodbye, World!', 15, 6.28); ");

Debug.WriteLine("Executing 4...");
db.ExecuteNonQuery("INSERT INTO Test (Var1) VALUES('Red'),('Blue'),('Green'),('White');");

Debug.WriteLine("Executing 5...");
var result1 = db.ExecuteQuery("SELECT Var1 FROM Test;");

Debug.WriteLine("Executing 6...");
var result2 = db.ExecuteQuery("SELECT Var1, Var2, Var3 FROM Test WHERE Var2 > 10;");

Debug.WriteLine("Executing 7...");
var result3 = db.ExecuteQuery("SELECT Var1, Var2, Var3 FROM Test WHERE Var2 BETWEEN 24 AND 26");

Debug.WriteLine("Executing 7...");
Debug.WriteLine(result2.ColumnCount.ToString() + " " +
result2.RowCount.ToString());

var str = "";

//foreach (var j in result1.ColumnNames)
// str += j + " ";

//Debug.WriteLine(str);

foreach (ArrayList i in result1.Data) {
str = "";

foreach (object j in i)
str += j.ToString() + " ";

Debug.WriteLine(str);

}

foreach (ArrayList i in result2.Data) {
str = "";

foreach (object j in i)
str += j.ToString() + " ";

Debug.WriteLine(str);
}

foreach (ArrayList i in result3.Data) {
str = "";

foreach (object j in i)
str += j.ToString() + " ";

Debug.WriteLine(str);
}
}

Omitted commands & features

In order to reduce the memory requirements, some commands and features of SQLite have been omitted. Below is a table of those omitted features. For more information regarding these omissions see the SQLite Website.

Omitted Command or Feature
ALTER TABLEExecuting the statement causes a parse error
ANALYZECommand omitted from the build
ATTACHATTACH and DETACH commands are omitted
REINDEXExecuting the statement causes a parse error
AUTOMATIC INDEXFeature omitted from the build
AUTHORIZATIONAuthorization callback feature omitted
AUTOINCREMENTFeature omitted from the build
AUTOVACCUMFeature omitted from the build
BLOB LITERALNot possible to specify a blob in an SQL statement using X'ABCD' syntax
CTECommon Table Expressions omitted from the build
DATETIME FUNCSSQL functions julianday(), date(), time(), datetime() and strftime() are not available
DEPRECATEDOmitted support for interfaces marked deprecated by SQLite
EXPLAINExecuting the statement causes a parse error
FLAG PRAGMASPRAGMA commands that query and set boolean properties omitted
FOREIGN KEYForeign key constraint syntax is not recognized
HEX INTEGERSupport for Hexadecimal integer literals omitted
INCRBLOBSupport for incremental BLOB I/O omitted
INTEGRITY CHECKSupport for integrity check pragma omitted
LIKE OPTIMIZATIONFeature to help resolve LIKE and GLOB operators in a WHERE clause omitted
LOAD EXTENSIONExtension loading mechanism omitted
LOCALTIME"localtime" modifier from the date and time functions omitted
LOOKASIDELookaside memory allocator omitted
OR OPTIMIZATIONIndex together with terms of a WHERE clause connected by the OR operator, disabled.
PAGER PRAGMASPragmas related to the pager subsystem omitted from the build
PRAGMAP ragma command has been omitted
PROGRESS CALLBACK"progress" callbacks capability during long-running SQL statements omitted.
QUICKBALANCEOmitted an alternative, faster B-Tree balancing routine.
SCHEMA PRAGMASPragmas for querying the database schema from the build omitted
SCHEMA VERSION PRAGMASPragmas for querying & modifying the database schema and user versions omitted
SHARED CACHESupport for shared-cache mode has been omitted
SUBQUERYSupport for sub-selects and the IN() operator are omitted.
TCL VARIABLE"$" syntax used to automatically bind SQL variables to TCL variables is omitted.
TEMPDBSupport for TEMP or TEMPORARY tables omitted
TRACESqlite3_profile() and sqlite3_trace() interfaces and their associated logic omitted
TRIGGERTRIGGER objects omitted. CREATE TRIGGER or DROP TRIGGER commands are unavailable
TRUNCATE OPTIMIZATIONRemoving this feature only affects the speed of operation
UTF16Support for UTF16 text encoding has been omitted.
VIRTUALTABLEVirtual Table mechanism in SQLite omitted.
XFER OPTRemoved optimization that help INSERT INTO...SELECT...run faster
WALWrite-ahead log capability omitted.

More Info

Further details on SQLite can be found at the official SQLite website http://www.sqlite.org/