Withdata Software

Store images in SQLite BLOB

SQLite BLOB Type Features

Step 1: Create a Table with BLOB Column

Basic Table (BLOB for Image Storage)

-- Create table in SQLite (via sqlite3 command line or SQLite Studio)
CREATE TABLE image_storage (
    image_id INTEGER PRIMARY KEY AUTOINCREMENT, -- Auto-increment integer primary key
    image_name TEXT NOT NULL, -- TEXT for cross-platform compatibility
    image_blob BLOB NOT NULL, -- BLOB for image binary data
    upload_time DATETIME DEFAULT CURRENT_TIMESTAMP
);

Optimized Table (With Index on Name for Fast Filtering)

CREATE TABLE image_storage (
    image_id INTEGER PRIMARY KEY AUTOINCREMENT,
    image_name TEXT NOT NULL,
    image_blob BLOB NOT NULL,
    upload_time DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- Add index on image_name (avoid indexing BLOB column)
CREATE INDEX idx_image_name ON image_storage(image_name);

Step 2: Insert Images into SQLite BLOB

Method 1: Using sqlite3 Command Line (Local File)

-- Prerequisite: Open SQLite database file (sqlite3 command line)
sqlite3 your_database.db;

-- Method A: Insert using readfile() function (SQLite 3.41.0+)
INSERT INTO image_storage (image_name, image_blob)
VALUES (
    'product_photo.jpg',
    readfile('/path/to/product_photo.jpg')
);

-- Method B: Insert using sqlite3 binary (batch mode, legacy compatible)
sqlite3 your_database.db "INSERT INTO image_storage (image_name, image_blob) VALUES ('product_photo.jpg', readfile('/path/to/product_photo.jpg'));"
COMMIT;

Note:

1. readfile() requires SQLite 3.41.0 or newer (use legacy methods for older versions)

2. File path must be absolute (or relative to the sqlite3 working directory)

3. For Windows paths: Use forward slashes (C:/images/product_photo.jpg) or double backslashes

Method 2: Using DBBlobEditor

Insert Single Image into SQLite BLOB

  1. Connect to your SQLite database in DBBlobEditor
  2. Navigate to the “image_storage” table in the target schema
  3. Click “+” button to add a record, and then Edit image stored in SQLite BLOB field

Batch Insert Images into SQLite BLOB

Step 3: Retrieve and Restore SQLite BLOB to Image

Method 1: Using SQLite Query + Python Script

-- Step 1: Get BLOB data (SQLite query)
SELECT image_blob FROM image_storage WHERE image_id = 1;

-- Step 2: Restore to file (Python script)
import sqlite3

# Connect to SQLite database file
conn = sqlite3.connect('your_database.db')
cur = conn.cursor()

# Fetch BLOB data
cur.execute("SELECT image_blob FROM image_storage WHERE image_id = 1")
blob_data = cur.fetchone()[0]

# Write to local file
with open("exported_photo.jpg", "wb") as f:
    f.write(blob_data)

# Cleanup
cur.close()
conn.close()

Method 2: DBBlobEditor Export

Retrieve and Restore Single Image

  1. Connect to SQLite database in DBBlobEditor
  2. Query the “image_storage” table and locate the target image_blob column
  3. Double click to View image stored in SQLite BLOB field
  4. Click menu “File” -> “Save” to save BLOB data as a local image (JPG/PNG/GIF/WEBP/etc.) file

Batch Export SQLite BLOB to Images

SQLite Specific Notes


Related Guides