Skip to content

Instantly share code, notes, and snippets.

@Igaryu
Last active October 19, 2023 08:29
Show Gist options
  • Save Igaryu/6a54d63002e6ae0315a6df1fa5ec9583 to your computer and use it in GitHub Desktop.
Save Igaryu/6a54d63002e6ae0315a6df1fa5ec9583 to your computer and use it in GitHub Desktop.
NIM procedure tha verify that the file opened is really an sqlite database.
# 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!!
import os
proc isSQLite3(filename: string): bool =
# Check if the file exists
if not os.isFile(filename):
return false
# Check if the file is at least 100 bytes long
# (SQLite database file header is 100 bytes long)
if os.getFileSize(filename) < 100:
return false
# Open the file and read the first 16 bytes
with open(filename, "rb") as fd:
header := fd.read(16)
# Check if the header matches the SQLite database file header
if header[:16] == "SQLite format 3\0":
return true
# The header does not match, so the file is not a SQLite database file
return false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment