Extremely fast, robust, and lightweight SQLite bindings for .NET and Unity
English | 日本語
CsSqlite is a highly performant and lightweight SQLite binding built in C#. It provides an API equivalent to Microsoft.Data.Sqlite (the foundation package for EFCore SQLite) while achieving high performance through carefully tuned implementations.
- Easy-to-use API
- High-performance implementation leveraging
Span<T>andUnsafe - Zero allocation, no additional memory allocation
- No dependencies
- Robust binding generation using Cysharp/csbindgen
- Supports Unity (Mono, IL2CPP)
CsSqlite requires .NET Standard 2.1 or later. The package is available on NuGet.
dotnet add package CsSqliteInstall-Package CsSqliteFor Unity, installation is possible via the Package Manager.
- Open the Package Manager from Window > Package Manager
- Click the "+" button > Add package from git URL
- Enter the following URL:
https://github.com/nuskey8/CsSqlite.git?path=src/CsSqlite.Unity/Assets/CsSqlite.Unity
Alternatively, open Packages/manifest.json and add the following to the dependencies block:
{
"dependencies": {
"com.nuskey.sqlite3.unity": "https://github.com/nuskey8/CsSqlite.git?path=src/CsSqlite.Unity/Assets/CsSqlite.Unity"
}
}CsSqlite.Unity supports the following platforms:
| Platform | Architecture | Supported | Notes |
|---|---|---|---|
| Windows | x64 | ✅ | |
| x86 | ✅ | ||
| arm64 | ✅ | ||
| macOS | x64 | ✅ | |
| arm64 (Apple Silicon) | ✅ | ||
| Universal (x64 + arm64) | ✅ | ||
| Linux | x64 | ✅ (untested) | |
| arm64 | ✅ (untested) | ||
| iOS | arm64 | ✅ | |
| x64 | ✅ | ||
| Android | arm64 | ✅ |
using CsSqlite;
// Open a SqliteConnection
using var connection = new SqliteConnection("example.db");
connection.Open();
// Execute SQL
connection.ExecuteNonQuery("""
CREATE TABLE IF NOT EXISTS user (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
age INTEGER NOT NULL,
name TEXT NOT NULL
);
""");
// Overload accepting UTF-8 text
connection.ExecuteNonQuery("""
INSERT INTO user (id, name, age)
VALUES (1, 'Alice', 18),
(2, 'Bob', 32),
(3, 'Charlie', 25);
"""u8);
// Create a reader
using var reader = connection.ExecuteReader("""
SELECT name
FROM user
""");
// Read values using Read() / GetXXX(column)
while (reader.Read())
{
Console.WriteLine($"{reader.GetString(0)}!");
}To reuse the same query, use SqliteCommand.
using var command = connection.CreateCommand("""
SELECT name
FROM user
""");
using var reader = command.ExecuteReader();You can also add parameters to SqliteCommand.
using var command = conn.CreateCommand("INSERT INTO t(val) VALUES($foo);");
command.Parameters.Add("$foo", "foo");
command.ExecuteNonQuery();If any error occurs during execution, a SqliteException is thrown. You can handle exceptions by catching this.
try
{
// ...
}
catch (SqliteException ex)
{
Console.WriteLine(ex.ErrorCode);
Console.WriteLine(ex.Message);
}This library is provided under the MIT License.
