Skip to content

Instantly share code, notes, and snippets.

@Igaryu
Last active August 10, 2023 09:19
Show Gist options
  • Save Igaryu/19abaa3c087deae2257e15d35024ffdd to your computer and use it in GitHub Desktop.
Save Igaryu/19abaa3c087deae2257e15d35024ffdd to your computer and use it in GitHub Desktop.
isSQLite3.py
def isSQLite3(filename: str) -> bool:
```
I needed this function because the `connect` function of the `sqlite3` python module DOES NOT,
and I repeat DOES NOT, verify that the file passed as a parameter is really an sqlite3 database file!
If you open grandma's soup recipe in recipe.txt the `connect` function will not raise an error!!!
The only way I found to REALLY determine if a file is, or isn't, an sqlite3 dbase file, is the following
method:
IF the first 16 bytes match the string b'SQLite format 3\x00' then it REALLY is a sqlite3 file!!
```
from os.path import isfile, getsize
if not isfile(filename):
return False
if getsize(filename) < 100: # SQLite database file header is 100 bytes long
return False
with open(filename, 'rb') as fd:
header = fd.read(100)
if (header[:16] == b'SQLite format 3\x00'):
return True
else:
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment