Saving images to SQLite database

import sqlite3

conn = sqlite3.connect("database.db")
cursor = conn.cursor()


## create a table to store images
cursor.execute(
    """CREATE TABLE IF NOT EXISTS images
             (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, image BLOB)"""
)
conn.commit()


name = "picture.jpeg"

# Convert digital data to binary format
with open(name, "rb") as file:
    blobData = file.read()


# Insert image data into database
conn.execute(
    "INSERT INTO images (name, image) VALUES (?,?)", (name, blobData)
)
conn.commit()


Send a Comment

Your email address will not be published.