This script allows saving files to a SQLite database table and retrieving them. Useful for storing binary file content in a structured queryable database.
The main.py script has the overall workflow. Key functions:
create_connection()
: Connect to the SQLite database filecreate_table()
: Create the table to hold file datainsert_file()
: Add a new file record to the databaseretrieve_file()
: Fetch file from database and save to output
The table stores these fields for each file:
- id - primary key
- filename - original file name
- filehash - md5 hash of file contents
- file - the binary file data as a BLOB
To save and retrieve a file:
python main.py
This will:
- Connect to the database file (myfiledb.db)
- Create the files table if not exists
- Insert the sample file into table
- Retrieve file from table and save to output folder
The output folder will hold the exported file.
The DB_FILE constant defines the database filename. New files to add are specified in the insert_file() function.
- sqlite3
- os
- hashlib
Happy Coding! 🕶️