---
title: "Run SQLite PRAGMA integrity_check in C#"
url: https://recursiveai.org/onelite/cookbook/check-sqlite-database-integrity/
description: "Run SQLite PRAGMA integrity_check in C# with OneLite. Open a database read-only, accept a single ok row, and print any problem messages without repairing it."
version: "1.0.0"
---

# Run SQLite PRAGMA integrity_check 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) · [GitHub](https://github.com/recursive-ai-oss/onelite-csharp) · [Docs](https://recursiveai.org/onelite/docs/) · [Download example (.zip)](https://recursiveai.org/onelite/cookbook/check-sqlite-database-integrity/check-sqlite-database-integrity.zip)

Check the structure of a local SQLite file without attempting repairs. The download’s `notes.sqlite3` has a `notes` table with two rows and an index on `title`.

OneLite 1.0.0 runs the structural checks of `integrity_check` but doesn’t separately verify that index contents match their tables.

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

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

```csharp
using RecursiveAI.OneLite;

using var db =
    Database.OpenReadOnly("notes.sqlite3");
using var conn = db.Connect();
using var check =
    conn.Prepare("PRAGMA integrity_check");
var rows = new List<string?>();
while (check.Step())
    rows.Add(check.GetString(0));

var passed = rows is ["ok"];
foreach (var row in rows)
    Console.WriteLine(row);
return passed ? 0 : 1;
```

[The example](https://recursiveai.org/onelite/cookbook/check-sqlite-database-integrity/check-sqlite-database-integrity.zip) is the full program below: unzip it and `dotnet run`. To run only the code above, paste it into Program.cs of a new `dotnet new console` project and `dotnet run`.

```text
ok
```

## The full program

Use `dotnet run -- path/to/database.sqlite3` to check your own file. The full program sends problem messages to standard error instead of standard output.

```csharp
using RecursiveAI.OneLite;

using var db = Database.OpenReadOnly(args.FirstOrDefault() ?? "notes.sqlite3");
using var connection = db.Connect();
using var check = connection.Prepare("PRAGMA integrity_check");
var messages = new List<string?>();
while (check.Step())
    messages.Add(check.GetString(0));

bool passed = messages is ["ok"];
if (passed)
    Console.WriteLine("ok");
else
{
    Console.Error.WriteLine("Integrity check failed:");
    foreach (var message in messages)
        Console.Error.WriteLine(message);
}
return passed ? 0 : 1;
```

```text
ok
```

An unreadable file or an error opening the database can throw before you get any result rows; that isn’t a passing check.

Already using Microsoft.Data.Sqlite? See the [OneLite Microsoft.Data.Sqlite and EF Core adapter docs](https://recursiveai.org/onelite/docs/adapter/).

## Variations

**Limit the problem details to five.** Use this `Prepare` call in place of the original. A database with no problems still returns `ok`.

```csharp
using var check =
    connection.Prepare("PRAGMA integrity_check(5)");
```

## Related recipes

- [Find SQLite foreign key violations in C#](https://recursiveai.org/onelite/cookbook/find-sqlite-foreign-key-violations/)
- [Open a SQLite database read-only in C#](https://recursiveai.org/onelite/cookbook/open-sqlite-database-read-only/)
- [Back up a SQLite database in C#](https://recursiveai.org/onelite/cookbook/backup-sqlite-database/)

## The short answer

Open the file with `Database.OpenReadOnly`, prepare `PRAGMA integrity_check`, and read all the rows in its single result column. One row containing exactly `ok` is a pass. Return a nonzero exit status for any other result, and preserve the messages instead of treating every failure as the same problem. The check doesn’t try to repair the file.

## What does SQLite integrity_check actually check?

[SQLite documents `integrity_check`](https://www.sqlite.org/pragma.html#pragma_integrity_check) as a low-level consistency check. OneLite 1.0.0 performs the structural part: it checks how the file’s supported table and index trees are laid out, including page types, key order, freeblocks, and overflow chains. It doesn’t separately verify that the contents of each index match its table or validate row values against every SQL constraint. Treat `ok` as the result of the checks this engine performed, not as a guarantee that the application’s data is correct.

Foreign key references need a separate check even in SQLite. If you want to find child rows with no matching parent, run [`PRAGMA foreign_key_check`](https://recursiveai.org/onelite/cookbook/find-sqlite-foreign-key-violations/) separately.

## What does it mean if integrity_check doesn’t return ok?

Problem rows describe what the engine found. OneLite puts a database name header before its structural error details, so the number of returned rows isn’t necessarily the number of errors. If the file doesn’t open, handle that as an error too, rather than reporting an empty result as success. When investigating a problem, keep the original intact and work from a copy; this recipe only diagnoses the file.
