Skip to content

Instantly share code, notes, and snippets.

@BlogBlocks
Last active July 18, 2018 03:34
Show Gist options
  • Select an option

  • Save BlogBlocks/382d56d56245df975c6d7d93b5c24837 to your computer and use it in GitHub Desktop.

Select an option

Save BlogBlocks/382d56d56245df975c6d7d93b5c24837 to your computer and use it in GitHub Desktop.
#Creating a database and inserting binaries
"""
Created to store sound for slide show clips. However it will store any files in binary
database. Great for images
"""
import sqlite3
def open_db(database):
conn = sqlite3.connect(database)
c=conn.cursor()
c.execute(
'''create table if not exists Binaries(
ID INTEGER PRIMARY KEY AUTOINCREMENT,
binaryD BLOB,
TYPE TEXT,
FILE_NAME TEXT);''')
def insert_binary(conn):
titlelist = "GISTstore/music.list"
titles = open(titlelist,"r")
for title in titles.readlines():
title = title.replace("\n","")
filename = os.path.basename(title)
with open(title, 'rb') as input_file:
ablob = input_file.read()
base=os.path.basename(title)
afile, ext = os.path.splitext(base)
sql = '''INSERT INTO Binaries
(binaryD, TYPE, FILE_NAME)
VALUES(?, ?, ?);'''
conn.execute(sql,[sqlite3.Binary(ablob), ext, afile])
conn.commit()
database = 'MP3/junk4.db'
open_db(database)
conn = sqlite3.connect(database)
insert_binary(conn)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment