Home , Post

SQLite Blob

By Nikolai Shokhirev http://www.numericalexpert.com./

In [1]:
import sqlite3
import os.path
from os import listdir, getcwd
from IPython.core.display import Image 

List files

In [2]:
def get_picture_list(rel_path):
    abs_path = os.path.join(os.getcwd(),rel_path)
    print 'abs_path =', abs_path
    dir_files = os.listdir(abs_path)
    #print dir_files
    return dir_files
In [3]:
picture_list = get_picture_list('pictures')
print picture_list
abs_path = E:\Projects\Numerical_Notes\SQLite\pictures
['Chrysanthemum50.jpg', 'Desert50.jpg', 'Hydrangeas50.jpg', 'Jellyfish50.jpg', 'Koala50.jpg', 'Lighthouse50.jpg', 'Penguins50.jpg', 'Thumbs.db', 'Tulips50.jpg']

In [4]:
Image(filename='.\pictures\Chrysanthemum50.jpg')
Out[4]:

Create picture database

In [5]:
def create_or_open_db(db_file):
    db_is_new = not os.path.exists(db_file)
    conn = sqlite3.connect(db_file)
    if db_is_new:
        print 'Creating schema'
        sql = '''create table if not exists PICTURES(
        ID INTEGER PRIMARY KEY AUTOINCREMENT,
        PICTURE BLOB,
        TYPE TEXT,
        FILE_NAME TEXT);'''
        conn.execute(sql) # shortcut for conn.cursor().execute(sql)
    else:
        print 'Schema exists\n'
    return conn
In [6]:
def insert_picture(conn, picture_file):
    with open(picture_file, 'rb') as input_file:
        ablob = input_file.read()
        base=os.path.basename(picture_file)
        afile, ext = os.path.splitext(base)
        sql = '''INSERT INTO PICTURES
        (PICTURE, TYPE, FILE_NAME)
        VALUES(?, ?, ?);'''
        conn.execute(sql,[sqlite3.Binary(ablob), ext, afile]) 
        conn.commit()
In [7]:
conn = create_or_open_db('picture_db.sqlite')
Creating schema

In [8]:
picture_file = "./pictures/Chrysanthemum50.jpg"
insert_picture(conn, picture_file)
In [9]:
conn.close()
In [10]:
def extract_picture(cursor, picture_id):
    sql = "SELECT PICTURE, TYPE, FILE_NAME FROM PICTURES WHERE id = :id"
    param = {'id': picture_id}
    cursor.execute(sql, param)
    ablob, ext, afile = cursor.fetchone()
    filename = afile + ext
    with open(filename, 'wb') as output_file:
        output_file.write(ablob)
    return filename
In [11]:
conn = create_or_open_db('picture_db.sqlite')
cur = conn.cursor()
filename = extract_picture(cur, 1)
cur.close()
conn.close()
Image(filename='./'+filename)
Schema exists


Out[11]:

Bulk insert

In [12]:
conn = create_or_open_db('picture_db.sqlite')
conn.execute("DELETE FROM PICTURES")
for fn in picture_list:
    picture_file = "./pictures/"+fn
    insert_picture(conn, picture_file)
    
for r in conn.execute("SELECT FILE_NAME FROM PICTURES"):
    print r[0]

conn.close()
Schema exists

Chrysanthemum50
Desert50
Hydrangeas50
Jellyfish50
Koala50
Lighthouse50
Penguins50
Thumbs
Tulips50

In []:

 

Home , Post