NativeSQLite 2.0.0
dotnet add package NativeSQLite --version 2.0.0
NuGet\Install-Package NativeSQLite -Version 2.0.0
<PackageReference Include="NativeSQLite" Version="2.0.0" />
<PackageVersion Include="NativeSQLite" Version="2.0.0" />
<PackageReference Include="NativeSQLite" />
paket add NativeSQLite --version 2.0.0
#r "nuget: NativeSQLite, 2.0.0"
#:package NativeSQLite@2.0.0
#addin nuget:?package=NativeSQLite&version=2.0.0
#tool nuget:?package=NativeSQLite&version=2.0.0
NativeSQLite
100% 纯 C# 实现的 SQLite 数据库引擎 —— 不是一个包装器,是一整个引擎。
没有 e_sqlite3.dll,没有 P/Invoke,没有任何原生依赖。一个 137 KB 的托管 dll,直接读写标准 SQLite 文件格式,与官方 sqlite3 生成的数据库文件完全互通——你的 .db 文件用 DB Browser、Navicat、Python sqlite3 随便打开,反过来也一样。
为什么选它
- 真·单文件部署:整个引擎就是一个
NativeSQLite.dll。x86、x64、ARM 通吃,再也没有runtimes\win-x64这种文件夹,没有平台架构的烦恼,发布时干净得不像数据库组件 - 零原生依赖:不装 VC++ 运行库,不挑系统,工控机、瘦客户机、干净 Windows 上扔过去就跑
- 标准文件格式:底层完整实现了 SQLite 的 B-Tree 页结构、记录编码和回滚日志,写出来的就是标准
.db文件,PRAGMA integrity_check随便过 - 双框架覆盖:
net8.0+.NET Framework 4.6.1,新项目和十几年前的老 WinForms 工程用同一个包 - 多实例友好:同一进程里多个地方
new Database(...)打开同一个文件,自动共享底层连接(引用计数),最后一个释放才真正关库 - 写入自动加速:内置事务合并(200ms 写入窗口 + 1 秒强制提交),高频小写入场景下性能成倍提升,
Dispose时保证落盘 - SQL 该有的都有:参数绑定防注入、事务、索引、聚合、JOIN、子查询、常用标量/日期函数
快速开始
using NativeSQLite;
// 打开(不存在则自动创建)
using var db = new Database("data.db");
就这么简单。没有连接字符串,没有驱动注册,没有 Initialize()。
增删改查教程
建表
db.Execute(@"
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
age INTEGER,
score REAL
)");
增(Insert)
永远用参数绑定,既防 SQL 注入,又不用操心单引号转义:
db.ExecuteNonQuery(
"INSERT INTO users (name, age, score) VALUES (?, ?, ?)",
"张三", 28, 91.5);
// 支持 string / int / long / double / null / byte[]
db.ExecuteNonQuery(
"INSERT INTO users (name, age, score) VALUES (?, ?, ?)",
"李四", 35, null); // null 就是 SQL 的 NULL
查(Select)
Query 返回 List<Dictionary<string, string>>,字典 key 不区分大小写:
// 查全部
var all = db.Query("SELECT * FROM users ORDER BY id");
foreach (var row in all)
Console.WriteLine($"{row["id"]}: {row["Name"]} = {row["SCORE"]}"); // key 大小写随意
// 条件查询 + 参数
var adults = db.Query(
"SELECT name, age FROM users WHERE age > ? ORDER BY age DESC",
30);
// 聚合
var stat = db.Query("SELECT COUNT(*) AS cnt, AVG(score) AS avg_score FROM users");
Console.WriteLine($"共 {stat[0]["cnt"]} 人, 平均分 {stat[0]["avg_score"]}");
注意:返回值都是字符串。
INTEGER列读出"28",REAL列读出"91.5",NULL读出空字符串""——这是 SQLite 的标准文本化行为,按需int.Parse/double.Parse即可。
改(Update)
int affected = db.ExecuteNonQuery(
"UPDATE users SET score = ? WHERE name = ?",
99.5, "李四");
Console.WriteLine($"更新了 {affected} 行");
删(Delete)
db.ExecuteNonQuery("DELETE FROM users WHERE name = ?", "王五");
事务
批量写入用手动事务,快且原子:
db.ExecuteNonQuery("BEGIN");
try
{
foreach (var (name, age) in new[] { ("赵六", 22), ("钱七", 31), ("孙八", 45) })
db.ExecuteNonQuery("INSERT INTO users (name, age) VALUES (?, ?)", name, age);
db.ExecuteNonQuery("COMMIT");
}
catch
{
db.ExecuteNonQuery("ROLLBACK");
throw;
}
不显式开事务时,每次写入会自动并入一个 200ms 的合并事务,到点或
Dispose时统一提交——这正是高频写入也很快的秘诀,你不用做任何事。
索引
db.Execute("CREATE INDEX IF NOT EXISTS idx_users_name ON users(name)");
完整示例
using System;
using NativeSQLite;
class Program
{
static void Main()
{
using (var db = new Database("demo.db"))
{
db.Execute("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER, score REAL)");
db.ExecuteNonQuery("INSERT INTO users (name, age, score) VALUES (?, ?, ?)", "张三", 28, 91.5);
db.ExecuteNonQuery("INSERT INTO users (name, age, score) VALUES (?, ?, ?)", "李四", 35, 82.0);
db.ExecuteNonQuery("UPDATE users SET score = ? WHERE name = ?", 99.5, "李四");
db.ExecuteNonQuery("DELETE FROM users WHERE age < ?", 30);
foreach (var row in db.Query("SELECT * FROM users"))
Console.WriteLine($"{row["name"]}, {row["age"]} 岁, {row["score"]} 分");
} // Dispose: 自动提交未落盘的事务并关闭文件
}
}
Execute / ExecuteNonQuery / Query 方法签名完全不变,改一行 new 就迁移完。
注意事项
- 引擎内部固定使用回滚日志模式;
PRAGMA journal_mode = WAL之类的语句会被接受(兼容旧代码不报错),但不会真的切换模式——对日常使用无影响,1.x 留下的 WAL 库文件可以直接打开继续用 - 写入后有最长 200ms 的合并延迟才落盘,
Dispose立即落盘。外部工具要在程序Dispose后或稍等片刻再查看 - 返回值统一为字符串,记得按需转换类型
- 进程内同一文件用多个
Database实例没问题;但多个进程同时写同一个文件不是它的设计目标(工控单机场景用不到)
License
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Framework | net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
-
.NETFramework 4.6.1
- System.ValueTuple (>= 4.5.0)
-
net8.0
- No dependencies.
NuGet packages (2)
Showing the top 2 NuGet packages that depend on NativeSQLite:
| Package | Downloads |
|---|---|
|
PCHMI
PCHMI工控组态控件,支持Modbus‑TCP、SIEMENS S7、KEYENCE HOSTLINK、PANASONIC MEWTOCOL、MITSUBISHI MC、OPC Ua、MQTT、BECKHOFF ADS、CODESYS V3、AB LogixE/IP、OMRON FINS E/IP等多种PLC通信协议,快速搭建上位机HMI画面。实时数据采集JSON、EXCEL、SQLITE、MYSQL、POSTGRESQL、SQLSERVER等统一数据库操作接口 |
|
|
PCHMI.Core
PCHMI工控组态控件,支持Modbus‑TCP、SIEMENS S7、KEYENCE HOSTLINK、PANASONIC MEWTOCOL、MITSUBISHI MC、OPC Ua、MQTT、BECKHOFF ADS、CODESYS V3、AB LogixE/IP、OMRON FINS E/IP等多种PLC通信协议,快速搭建上位机HMI画面。实时数据采集JSON、EXCEL、SQLITE、MYSQL、POSTGRESQL、SQLSERVER等统一数据库操作接口 |
GitHub repositories
This package is not used by any popular GitHub repositories.