sqlite-net2 1.0.0-pre1

This is a prerelease version of sqlite-net2.
There is a newer version of this package available.
See the version list below for details.

Requires NuGet 2.5 or higher.

dotnet add package sqlite-net2 --version 1.0.0-pre1
NuGet\Install-Package sqlite-net2 -Version 1.0.0-pre1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="sqlite-net2" Version="1.0.0-pre1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add sqlite-net2 --version 1.0.0-pre1
#r "nuget: sqlite-net2, 1.0.0-pre1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install sqlite-net2 as a Cake Addin
#addin nuget:?package=sqlite-net2&version=1.0.0-pre1&prerelease

// Install sqlite-net2 as a Cake Tool
#tool nuget:?package=sqlite-net2&version=1.0.0-pre1&prerelease

Setup

Add this package to your netstandard project:

TODO

And call this function in your executable projects (.net, ios, android, uwp, mac, ...):

SQLitePCL.Batteries_V2.Init()

See https://github.com/ericsink/SQLitePCL.raw/ for more information on how to use SQLitePCL.raw

If you search a simple key value store based on sqlite, alternative to Akavache, check https://github.com/softlion/KeyValueLite

Changes

  • Netstandard 2.0 only
  • Uses SQLitePCLRaw for sqlite raw communication

New Features compared to oysteinkrog

Multiple primary key support Ex:

 public class PrivacyGroupItem		
 {		
	[PrimaryKey]		
	public int GroupId {get;set;}		
	
	[PrimaryKey]		
	public int ContactId {get;set;}		
 }		
	
 db.Delete<PrivacyGroupItem>(groupId, contactId);		
	
	

Projections now have the expected result type Ex: IEnumerable<int> ids = from pgi in db.Table<PrivacyGroupItem>() where pgi.PrivacyGroupId == groupId select pgi.ContactId;

New method to query simple types (ie: string, ...) as Query<T> can query only complex types (ie: T must be a class/stuct with a default constructor) Signature: IEnumerable<T> ExecuteSimpleQuery<T>(string query, params object[] args) Usage: ExecuteSimpleQuery<string>("select 'drop table ' || name || ';' from sqlite_master where type = 'table'")

Original Fork

https://github.com/praeclarum/sqlite-net

Examples

Please consult the source code (see unit tests) for more examples.

The library contains simple attributes that you can use to control the construction of tables. In a simple stock program, you might use:

public class Stock
{
	[PrimaryKey, AutoIncrement]
	public int Id { get; set; }
	[MaxLength(8)]
	public string Symbol { get; set; }
}

public class Valuation
{
	[PrimaryKey, AutoIncrement]
	public int Id { get; set; }
	[Indexed]
	public int StockId { get; set; }
	public DateTime Time { get; set; }
	public decimal Price { get; set; }
}

Once you've defined the objects in your model you have a choice of APIs. You can use the "synchronous API" where calls block one at a time, or you can use the "asynchronous API" where calls do not block. You may care to use the asynchronous API for mobile applications in order to increase reponsiveness.

Both APIs are explained in the two sections below.

Synchronous API

Once you have defined your entity, you can automatically generate tables in your database by calling CreateTable:

var db = new SQLiteConnection(sqlitePlatform, "foofoo");
db.CreateTable<Stock>();
db.CreateTable<Valuation>();

You can insert rows in the database using Insert. If the table contains an auto-incremented primary key, then the value for that key will be available to you after the insert:

public static void AddStock(SQLiteConnection db, string symbol) {
	var s = db.Insert(new Stock() {
		Symbol = symbol
	});
	Console.WriteLine("{0} == {1}", s.Symbol, s.Id);
}

Similar methods exist for Update and Delete.

The most straightforward way to query for data is using the Table method. This can take predicates for constraining via WHERE clauses and/or adding ORDER BY clauses:

	var conn = new SQLiteConnection(sqlitePlatform, "foofoo");
	var query = conn.Table<Stock>().Where(v => v.Symbol.StartsWith("A"));

	foreach (var stock in query)
		Debug.WriteLine("Stock: " + stock.Symbol);

You can also query the database at a low-level using the Query method:

public static IEnumerable<Valuation> QueryValuations (SQLiteConnection db, Stock stock)
{
	return db.Query<Valuation> ("select * from Valuation where StockId = ?", stock.Id);
}

The generic parameter to the Query method specifies the type of object to create for each row. It can be one of your table classes, or any other class whose public properties match the column returned by the query. For instance, we could rewrite the above query as:

public class Val {
	public decimal Money { get; set; }
	public DateTime Date { get; set; }
}
public static IEnumerable<Val> QueryVals (SQLiteConnection db, Stock stock)
{
	return db.Query<Val> ("select 'Price' as 'Money', 'Time' as 'Date' from Valuation where StockId = ?", stock.Id);
}

You can perform low-level updates of the database using the Execute method.

Asynchronous API

The asynchronous API has been removed, as it was only wrapping synchronous methods in Task.Run(), which has nasty side effects as multiple Tasks are queued. Use your own Task.Run to achieve the same effect.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on sqlite-net2:

Package Downloads
vapolia-keyvaluelite

A netstandard key value store, backed by sqlite, alternative to Akavache

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.0 229 2/26/2024
2.1.0-preB 1,076 5/18/2022
2.1.0-preA 160 4/29/2022
2.1.0-pre9 145 4/29/2022
2.1.0-pre8 142 4/29/2022
2.1.0-pre7 149 4/28/2022
2.1.0-pre6 150 4/27/2022
2.1.0-pre4 149 4/27/2022
2.1.0-pre3 158 4/26/2022
2.1.0-pre2 157 4/26/2022
2.1.0-pre1 156 4/25/2022
2.0.7 827 1/27/2022
2.0.6 423 9/22/2021
2.0.4 615 3/19/2021
2.0.3 826 5/17/2020
2.0.2 650 3/21/2020
2.0.0 1,988 8/19/2019
1.0.0 1,208 4/15/2019
1.0.0-pre1 438 4/15/2019