Recovery salvages surviving records from a damaged file; it does not guarantee a repaired database.
dotnet add package RecursiveAI.OneLiteRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneLite.Storage.Forensics; // damaged.db is included in the example. using var db = ForensicDatabase.Open("damaged.db"); var snapshot = db.OpenSnapshot(); foreach (var table in snapshot.Tables.Where(t => t.CanReadRows)) foreach (var row in snapshot.ReadRows(table)) Console.WriteLine(string.Join(", ", row.Values));
1, book 2, tool
Unzip the example, then dotnet run.
Full program
damaged.db in the zip is a synthetic, freely redistributable fixture. It contains items(id INTEGER PRIMARY KEY, name TEXT) with (1, 'book') and (2, 'tool'). Its index page was replaced with zeros; the schema and table page remain intact. The example never modifies this input.
using RecursiveAI.OneLite.Storage.Forensics; using var database = ForensicDatabase.Open("damaged.db"); var snapshot = database.OpenSnapshot(); foreach (var table in snapshot.Tables.Where(t => t.CanReadRows)) { Console.WriteLine($"Table: {table.Name}; root page: {table.RootPage}"); foreach (var row in snapshot.ReadRows(table)) Console.WriteLine($"Row {row.Rowid}: {string.Join(", ", row.Values)}"); } Console.WriteLine($"Uncommitted: {snapshot.IncludesUncommitted}"); Console.WriteLine($"Carved candidates: {snapshot.Carve().Count}"); foreach (var note in snapshot.Notes) Console.WriteLine(note);
Table: items; root page: 2 Row 1: 1, book Row 2: 2, tool Uncommitted: False Carved candidates: 0
An invalid database header throws NotADatabaseException; malformed B-tree pages can throw CorruptDatabaseException. The forensic reader never modifies the file set.
Variations
Inspect only database pages, without overlaying the write-ahead log (replacing database pages with their newer logged versions). Add these lines at the end of the full program.
var databaseOnly = database.OpenSnapshot(SnapshotSelector.DatabaseOnly); Console.WriteLine($"May be newer: {databaseOnly.DatabaseMayBeNewer}"); Console.WriteLine($"Uncommitted: {databaseOnly.IncludesUncommitted}");
May be newer: False Uncommitted: False
Print carved candidates with their proposed table, rowid, confidence and values. Add these lines at the end of the full program.
foreach (var candidate in snapshot.Carve()) Console.WriteLine($"{candidate.Table}; rowid={candidate.Rowid}; " + $"confidence={candidate.Confidence}; " + string.Join(", ", candidate.Values));
This fixture produces no lines from this loop: it has no carved candidates. Its surviving table rows are printed by ReadRows, not by carving.
Related recipes
The short answer
Use ForensicDatabase.Open and OpenSnapshot() to read surviving rows through OneLite’s read-only forensic reader. Enumerate tables with CanReadRows, then call ReadRows(table). This recovers values for inspection; it does not rebuild indexes, write SQL or produce a repaired database file.
The example deliberately damages an index page while leaving the table readable. Recovering both rows in that fixture says nothing about the recoverability of an overwritten table page. The complete program prints the table’s root page and rowids so the output remains connected to the schema object it came from.
How do I recover a corrupt SQLite database?
Work on a copy of the database file set. Keep its -wal, -shm and -journal files together when they exist, since the reader examines siblings beside the database path. The forensic API opens files read-only without requesting database locks, reads logs without applying them and never checkpoints. These behaviors are specified in the forensic reader documentation.
Open the default snapshot first. It selects the latest valid commit from a usable write-ahead log, or the database file when there is no usable log. Read recognizable tables and preserve their names, rowids and declared column order alongside exported values. A virtual table whose CREATE statement the schema parser does not understand has CanReadRows == false; skipping it is not evidence that it held no data.
A row read from a surviving B-tree and a candidate guessed from unused bytes are different kinds of evidence. Keep them separate. Do not automatically insert every candidate into a replacement production database.
Can SQLite recover deleted rows?
OneLite’s Carve() searches space that no live row occupies, including freeblocks (unused regions within pages), freelist pages (pages available for reuse), lost pages (pages neither reachable from a root nor listed as free), older page images and the file tail. The returned CarvedRecord includes file and byte offset, page, possible table and rowid, confidence, scoring reasons and flags such as Truncated and PrefixReconstructed.
Confidence is evidence that the bytes form a genuine record of a particular table, not a promise that the values are current or correct. Table association can be ambiguous; OtherTableCandidates reports other close matches. The default minimum confidence is 0.5. A higher threshold discards lower-scoring candidates but cannot turn speculation into certainty. The example finds no carved candidates; its two printed rows come from the intact table.
Can a corrupted SQLite database be fully recovered?
Not in general. SQLite’s recovery documentation explains that overwritten content can be unrecoverable, deleted content can reappear, and recovered values and constraints can be wrong. Review recovered data against application knowledge before using it again.
OneLite’s snapshot also carries Notes, DatabaseMayBeNewer and IncludesUncommitted to identify qualifications about the selected view. Older commits can be incomplete when later checkpoints have overwritten database pages. A snapshot explicitly including trailing frames includes uncommitted data; do not label it committed history.
Malformed headers or B-trees can still prevent reads. Space() continues past anomalies in its page classification, but ReadRows() is not a universal bypass for corrupt B-tree structure. Preserve the original file set even when the first pass yields nothing: zero returned candidates does not establish that all recoverable evidence has been exhausted.