SQLitePCLRaw Documentation
repository·main·Indexed 20 days ago
https://github.com/ericsink/sqlitepcl.rawA low-level .NET Portable Class Library (PCL) providing raw access to the SQLite C API. It serves as a portable foundation for higher-level libraries like Microsoft.Data.Sqlite and sqlite-net-pcl. The library uses a decoupled architecture consisting of a core assembly, provider packages (e.g., e_sqlite3, sqlcipher), and native library packages such as SourceGear.sqlite3. It follows SQLite's C stylistic conventions for naming and error handling.
What's inside SQLitePCLRaw
- SQLitePCLRaw is a .NET Portable Class Library (PCL) designed for low-level (raw) access to SQLite. It is an open-source project licensed under Apache License v2 and is widely used in the .NET ecosystem.
Key changes in SQLitePCLRaw 3.0
mainThe 3.0 release introduces several architectural changes:
- Decoupled Configuration: A new
SQLitePCLRaw.config.e_sqlite3package allows users to choose their own native SQLite library without being forced into a specific bundle. - New Native Package Naming: The native library package is now
SourceGear.sqlite3(replacingSQLitePCLRaw.lib.e_sqlite3). - Version Alignment: The version number of native packages (like
SourceGear.sqlite3) now matches the version of the SQLite engine itself, rather than the SQLitePCLRaw version. - Removed Packages:
bundle_green,bundle_e_sqlite3mc,provider.e_sqlite3mc,bundle_zetetic,bundle_sqlite3, andbundle_winsqlite3have been removed. - Target Framework: The minimum targeted .NET Framework has changed from 4.6.2 to 4.7.1.
- Provider Mechanism: The default provider for .NET Framework has reverted to using
DLLImportinstead of dynamic loading.
- Decoupled Configuration: A new
Encryption options in SQLitePCLRaw 3.0
mainAs of version 3.0, SQLitePCLRaw no longer distributes no-cost SQLite builds with encryption. The
.bundle_e_sqlcipherpackage is deprecated.Users requiring encryption have the following options:
- SQLite Encryption Extension (SEE): The official implementation from the SQLite core team. Requires a paid license. SEE builds can be obtained via SourceGear's SQLite build service.
- SQLCipher: Supported builds can be purchased from Zetetic.
- SQLite3 Multiple Ciphers (sqlite3mc): An open-source option. Nuget packages for
sqlite3mcare available through SourceGear's SQLite build service.
Note: SourceGear's
e_sqlite3base name is compatible withsqlite3mcbuilds.Understand the SQLitePCLRaw API design
mainSQLitePCLRaw is a "raw" low-level wrapper around the SQLite C API. It is intentionally designed to follow SQLite's C stylistic conventions rather than standard .NET patterns.
Key characteristics for developers:
- Naming: Methods follow C naming conventions (e.g.,
sqlite3_open()instead ofSqlite3Open()). - Error Handling: Methods return integer error codes rather than throwing .NET exceptions.
- Purpose: It is designed as a portable foundation for higher-level wrappers (like
Microsoft.Data.Sqliteorsqlite-net-pcl) rather than for direct use in application-level code.
- Naming: Methods follow C naming conventions (e.g.,
Implement SQLite encryption support
mainSQLitePCLRaw does not provide free encryption-enabled builds. For encryption support, the recommended solution is the SQLite Encryption Extension (SEE), which is the official implementation from the SQLite team.
Note that SEE is not open source and requires a paid license. You can obtain SEE builds in the form of NuGet packages through SourceGear's SQLite build service.
How SQLitePCLRaw packages work together
mainUsing SQLitePCLRaw requires a combination of three types of packages to function correctly:
- Core Assembly:
SQLitePCLRaw.coreprovides the main logic but contains no providers. - Provider Package: A package with an ID like
SQLitePCLRaw.provider.*that implements theISQLite3Providerinterface. This tells the core assembly which native library to use. - Native Library Package: A package containing the actual native SQLite binaries (e.g.,
SourceGear.sqlite3).
In your platform-specific code, you must initialize the provider using
SQLitePCL.raw.SetProvider().// Example of manual initialization SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_e_sqlite3());- Core Assembly:
Configure encryption support with SQLitePCLRaw
mainTo use an encryption-enabled SQLite build, the recommended approach is to name your shared library
e_sqlite3and use theSQLitePCLRaw.config.e_sqlite3package. This allows you to swap in different crypto-enabled native libraries.Supported encryption options include:
- SQLite Encryption Extension (SEE): The official implementation from the SQLite team (requires a paid license).
- SQLCipher: Builds are available for purchase from Zetetic.
- SQLite3 Multiple Ciphers: Maintained by @utelle; builds are available via SourceGear's SQLite build service.
Obtain native SQLite builds via SourceGear
mainFor users requiring specific native SQLite builds, SourceGear provides a paid service at
nuget.sourcegear.com. This service offers:- Native SQLite builds updated immediately following each SQLite release.
- Regular SQLite builds.
- Builds with encryption support.
- Signed builds and supply chain information (e.g., SBOMs).
- Custom configurations.
Use system SQLite on iOS only
mainIf your project targets only iOS and you wish to use the SQLite library provided by the operating system instead of a bundled version, you can use the core and provider packages directly and manually initialize the provider.
<!-- Package references --> <PackageReference Include="sqlitepclraw.core" Version="3.0.1" /> <PackageReference Include="sqlitepclraw.provider.sqlite3" Version="3.0.1" /> <!-- Initialization code --> SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_sqlite3());Update to SQLitePCLRaw 3.0 from bundle_e_sqlite3
mainIf you are currently using
SQLitePCLRaw.bundle_e_sqlite3, the upgrade to 3.0 is a direct version bump and should 'Just Work'.However, for better control over SQLite updates, it is recommended to switch from the bundle package to the new decoupled pattern. This allows you to update the native SQLite library independently of the SQLitePCLRaw configuration by updating only the
SourceGear.sqlite3package.<!-- Option 1: Simple version bump --> <PackageReference Include="sqlitepclraw.bundle_e_sqlite3" Version="3.0.1" /> <!-- Option 2: Recommended decoupled approach --> <PackageReference Include="sqlitepclraw.config.e_sqlite3" Version="3.0.1" /> <PackageReference Include="sourcegear.sqlite3" Version="3.50.3" />Migrate from SQLitePCLRaw.bundle_green to e_sqlite3
mainThe
SQLitePCLRaw.bundle_greenpackage has been removed. This package was used to reference the system-provided SQLite on iOS while usinge_sqlite3on other platforms.To achieve similar behavior with the new 3.0 structure, it is recommended to switch to the pairing of
SQLitePCLRaw.config.e_sqlite3andSourceGear.sqlite3for all platforms.<!-- Recommended replacement for bundle_green --> <PackageReference Include="sqlitepclraw.config.e_sqlite3" Version="3.0.1" /> <PackageReference Include="sourcegear.sqlite3" Version="3.50.3" />Run the provider.tt T4 template
mainTo generate provider code from the
provider.tttemplate, use thet4command. Note that running this viadotnet t4may fail; use the directt4command instead.Example command:
t4 -o tmp.cs -p:NAME=tmp -p:CONV=Cdecl -p:KIND=dynamic provider.tt