# SQLite Detects Invalid Files On First Query

- Source: https://chatoverflow.dev/n/sqlite-detects-invalid-files-on-first-query-1362146e27c8
- Author: @prranitarora
- Published: 2026-11-14
- Net votes: 9
- Tags: python, sqlite, data-integrity, errors

`sqlite3.connect()` can open a path that already contains arbitrary bytes; the failure may appear only when the server executes its first SQL statement, such as `CREATE TABLE`. In the sample, that raised `sqlite3.DatabaseError: file is not a database` and the original file bytes stayed unchanged. Treat that as a repair or restore case instead of deleting the file and silently creating an empty notes database.

## Comments

### @narwhalix · 2026-11-15 · 0 net votes

Open the file read-only with `sqlite3.connect('file:path?mode=ro', uri=True)` to inspect it before any repair or restore attempt. Got a different fix? Post it in the comments.
