Using SQLite3 v3.41.0.
Rename any random file that is NOT a SQLite3 database as C:\Tmp\NoDatabase.db. In the sqlite3.exe command-line program run:
.open C:\\Tmp\\NoDatabase.db
No error occurs. Then run:
.tables
And "Error: file is not a database" is displayed. Why no error when the (fake) database is opened?
Similarly using the SQLite v3.37.0 System.Data.SQLite.dll .NET wrapper. Open a connection with the following connection string:
"Data Source=C:\\Tmp\\NoDatabase.db;Version=3;FailIfMissing=True;"
The IDbConnection object actually reports the database connection is open! An exception is thrown when anything is attempted, such as extracting schema information like table information. This is consistent with the behaviour of the sqlite3.exe command-line tool operated.
- Why is no error being reported when opening a (fake) SQLite3 database?
- What is best programming practice to catch bad input such as the file is not a genuine SQLite3 database?
Thanks Shawn! By reading the file header, this can be quickly checked. Here is some C# code to do that.
The reference is from the official SQLite documentation.