Skip to main content

SQLite Database

SQLite is a self-contained, serverless, single-file SQL database engine — the most widely deployed database engine in the world. TinyCLR includes a port of SQLite that's well-suited to embedded use cases where you need structured queries: data loggers, configuration stores, recipe tables, lookup data for sensor calibration, and similar.

The database can live entirely in RAM (fast, lost on power-off) or on persistent storage like an SD card or USB drive — same code either way; only the connection string changes.

NuGet package: GHIElectronics.TinyCLR.Data.SQLite.

note

TinyCLR's SQLite is a trimmed subset of the full engine. Several commands and features are omitted to reduce memory footprint — see Omitted features for the full list. Most standard SQL works; just don't expect ALTER TABLE, TRIGGER, FOREIGN KEY, subqueries, etc.

Create, insert, query

The example below creates an in-memory database, builds a table, inserts a few rows, runs three queries, and prints the results.

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

using (var db = new SQLiteDatabase()){
db.ExecuteNonQuery("CREATE TABLE Test (Var1 TEXT, Var2 INTEGER, Var3 DOUBLE);");

db.ExecuteNonQuery("INSERT INTO Test (Var1, Var2, Var3) VALUES ('Hello, World!', 25, 3.14);");
db.ExecuteNonQuery("INSERT INTO Test (Var1, Var2, Var3) VALUES ('Goodbye, World!', 15, 6.28);");
db.ExecuteNonQuery("INSERT INTO Test (Var1) VALUES ('Red'), ('Blue'), ('Green'), ('White');");

// Select every Var1 value.
PrintResult(db.ExecuteQuery("SELECT Var1 FROM Test;"));

// Filter with WHERE.
PrintResult(db.ExecuteQuery("SELECT Var1, Var2, Var3 FROM Test WHERE Var2 > 10;"));

// Range filter with BETWEEN.
PrintResult(db.ExecuteQuery("SELECT Var1, Var2, Var3 FROM Test WHERE Var2 BETWEEN 24 AND 26;"));
}

void PrintResult(ResultSet result){
Debug.WriteLine(result.RowCount + " row(s), " + result.ColumnCount + " column(s)");

foreach (ArrayList row in result.Data){
var line = "";
foreach (object value in row)
line += value.ToString() + " ";

Debug.WriteLine(line);
}
}

ResultSet.ColumnNames gives you the names of each column if you want to print a header row, and ResultSet.Data is a list of ArrayList rows where each ArrayList holds the column values in the same order as ColumnNames.

Omitted features

To keep the binary size manageable, TinyCLR's SQLite drops a number of commands and features. See the SQLite compile-time options for what each option does in the upstream engine.

Omitted command or featureBehavior
ALTER TABLEExecuting causes a parse error
ANALYZEOmitted from the build
ATTACH / DETACHATTACH and DETACH commands are omitted
REINDEXExecuting causes a parse error
AUTOMATIC INDEXFeature omitted from the build
AUTHORIZATIONAuthorization callback feature omitted
AUTOINCREMENTFeature omitted from the build
AUTOVACUUMFeature omitted from the build
BLOB LITERALNot possible to specify a blob in SQL using X'ABCD' syntax
CTECommon Table Expressions omitted
DATETIME FUNCSjulianday(), date(), time(), datetime(), strftime() not available
DEPRECATEDSupport for interfaces marked deprecated by SQLite is omitted
EXPLAINExecuting causes a parse error
FLAG PRAGMASPRAGMA commands that query/set boolean properties omitted
FOREIGN KEYForeign-key constraint syntax not recognized
HEX INTEGERHexadecimal integer literals omitted
INCRBLOBIncremental BLOB I/O omitted
INTEGRITY CHECKIntegrity-check pragma omitted
LIKE OPTIMIZATIONOptimization for LIKE and GLOB in WHERE clauses omitted
LOAD EXTENSIONExtension loading mechanism omitted
LOCALTIMElocaltime modifier from date/time functions omitted
LOOKASIDELookaside memory allocator omitted
OR OPTIMIZATIONIndex optimization for terms connected by OR disabled
PAGER PRAGMASPragmas related to the pager subsystem omitted
PRAGMAPRAGMA command has been omitted
PROGRESS CALLBACKProgress callbacks during long-running SQL statements omitted
QUICKBALANCEAlternative faster B-Tree balancing routine omitted
SCHEMA PRAGMASPragmas for querying the schema omitted
SCHEMA VERSION PRAGMASPragmas for querying/modifying schema and user versions omitted
SHARED CACHEShared-cache mode omitted
SUBQUERYSub-selects and IN() operator omitted
TCL VARIABLE$-prefix binding for TCL variables omitted
TEMPDBTEMP / TEMPORARY tables omitted
TRACEsqlite3_profile() / sqlite3_trace() interfaces omitted
TRIGGERCREATE TRIGGER / DROP TRIGGER unavailable
TRUNCATE OPTIMIZATIONSpeed optimization removed (functionally equivalent)
UTF16UTF-16 text encoding omitted (UTF-8 only)
VIRTUALTABLEVirtual Table mechanism omitted
XFER OPTOptimization for INSERT INTO ... SELECT ... removed
WALWrite-ahead log capability omitted

More information

The official SQLite documentation at sqlite.org covers the SQL syntax, data types, and engine behavior — all of which apply to TinyCLR's port except where noted above.

API reference

NamespaceDescription
GHIElectronics.TinyCLR.Data.SQLiteSQLite database engine classes