---
title: "Get SQLite extended error codes in C#"
url: https://recursiveai.org/onelite/cookbook/inspect-sqlite-ado-net-error-codes/
description: "Get primary and extended SQLite error codes from a OneLite ADO.NET exception. Reproduce a UNIQUE constraint error in an in-memory C# example."
version: "1.0.0"
---

# Get SQLite extended error codes in C#

**OneLite** is a free, open-source SQLite-compatible database for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneLite.Data) · [GitHub](https://github.com/recursive-ai-oss/onelite-csharp) · [Docs](https://recursiveai.org/onelite/docs/) · [Download example (.zip)](https://recursiveai.org/onelite/cookbook/inspect-sqlite-ado-net-error-codes/inspect-sqlite-ado-net-error-codes.zip)

Read `ExtendedResultCode` from a OneLite `DbException` to identify the specific SQLite failure.

```shell
dotnet add package RecursiveAI.OneLite.Data
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using System.Data.Common;
using RecursiveAI.OneLite.Data;

using DbConnection db =
    new OneLiteConnection(
        "Data Source=:memory:");
db.Open();
using DbCommand cmd = db.CreateCommand();
cmd.CommandText = "CREATE TABLE users " +
    "(email TEXT UNIQUE)";
cmd.ExecuteNonQuery();
cmd.CommandText = "INSERT INTO users " +
    "VALUES ('a@example.com')";
cmd.ExecuteNonQuery();
try
{
    cmd.ExecuteNonQuery();
}
catch (DbException ex)
    when (ex is OneLiteDataException sqlite)
{
    int primary = sqlite.ResultCode;
    int extended = sqlite.ExtendedResultCode;
    Console.WriteLine($"Primary: {primary}");
    Console.WriteLine($"Extended: {extended}");
}
```

The database exists only in memory and the SQL input is in the program. The first `INSERT` succeeds. The second throws `OneLiteDataException`, which derives from `DbException`; the filter lets unrelated exceptions propagate.

Paste the code above into Program.cs of a new `dotnet new console` project and `dotnet run`, or unzip [the example](https://recursiveai.org/onelite/cookbook/inspect-sqlite-ado-net-error-codes/inspect-sqlite-ado-net-error-codes.zip) and `dotnet run`.

Inserting the same email twice prints:

```text
Primary: 19
Extended: 2067
```

## Variations

**Catch the exact UNIQUE failure.** Replace the example’s `catch` clause with:

```csharp
catch (OneLiteDataException error)
    when (error.ExtendedResultCode == 2067)
{
    Console.WriteLine("Email already exists.");
}
```

## Related recipes

- [Use SQLite with ADO.NET in C#](https://recursiveai.org/onelite/cookbook/sqlite-ado-net-csharp/)
- [Insert or update a SQLite row in C#](https://recursiveai.org/onelite/cookbook/upsert-sqlite-row-csharp/)

## The short answer

When a command uses the OneLite ADO.NET provider, match the caught `DbException` to `OneLiteDataException`. Read `ExtendedResultCode` for the specific failure and `ResultCode` for its family. `SqliteExtendedErrorCode` and `SqliteErrorCode` are aliases for the same values. Use the numeric properties to choose what to handle instead of matching the English error message.

## What is the difference between a primary and an extended SQLite error code?

The [SQLite result code list](https://sqlite.org/rescode.html) names 19 as `SQLITE_CONSTRAINT` and 2067 as `SQLITE_CONSTRAINT_UNIQUE`. A general constraint handler can use 19, but it should not assume that every failure in that family means a duplicate value. For example, a failed foreign-key constraint has a different extended code. Handle only the specific case your application knows how to recover from; leave other errors for the caller or show a separate diagnostic. If OneLite has no more specific code for the error, `ExtendedResultCode` equals `ResultCode`.

## What does DbException.ErrorCode return for a SQLite error?

On `OneLiteDataException`, `ErrorCode` is an override that returns the primary result code, so in this example it reports 19, not 2067. The base [`DbException` type](https://learn.microsoft.com/en-us/dotnet/api/system.data.common.dbexception) gives you a common way to catch database errors, not a provider-independent SQLite code. If the connection can come from several providers, branch on its actual exception type before reading provider-specific codes. `OneLiteDataException` also exposes `Detail` for extra diagnostics; leave it as diagnostic text rather than using it to classify the failure.

If you catch `SqliteException` from `Microsoft.Data.Sqlite` rather than `OneLiteDataException`, see the [Microsoft.Data.Sqlite and EF Core guide for the SQLitePCLRaw adapter](https://recursiveai.org/onelite/docs/adapter/).
