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.
dotnet add package RecursiveAI.OneLiteRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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 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.
okThe 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.
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;
okAn 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.
Variations
Limit the problem details to five. Use this Prepare call in place of the original. A database with no problems still returns ok.
using var check = connection.Prepare("PRAGMA integrity_check(5)");
Related recipes
- Find SQLite foreign key violations in C#
- Open a SQLite database read-only in C#
- Back up a SQLite database in C#
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 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 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.