Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sqlite
#1
import sqlite3

def convertToBinaryData(filename):
    #Convert digital data to binary format
    with open(filename, 'rb') as file:
        blobData = file.read()
    return blobData

def insertBLOB(empId, name, photo):
    try:
        sqliteConnection = sqlite3.connect('SQLite_Python.db')
        cursor = sqliteConnection.cursor()
        print("Connected to SQLite")
        sqlite_insert_blob_query = """ INSERT INTO new_employee
                                  (id, name, photo) VALUES (?, ?, ?)"""

        empPhoto = convertToBinaryData(photo)
        
        # Convert data into tuple format
        data_tuple = (empId, name, empPhoto)
        cursor.execute(sqlite_insert_blob_query, data_tuple)
        sqliteConnection.commit()
        print("File inserted successfully as a BLOB into a table")
        cursor.close()

    except sqlite3.Error as error:
        print("Failed to insert blob data into sqlite table", error)
    finally:
        if (sqliteConnection):
            sqliteConnection.close()
            print("the sqlite connection is closed")
            
            # 1. Opera 

insertBLOB(2, r"emkit", r"C:\Users\CASH OFFICE 3\Desktop\EEK.zip")
----------------------------------------------------------------------------------------

The above code gives the following output:

Output:
Connected to SQLite the sqlite connection is closed Traceback (most recent call last): File "E:\KP\Newpy\blb_ins.py", line 35, in <module> insertBLOB(2, r"emkit", r"C:\Users\CASH OFFICE 3\Desktop\EEK.zip") File "E:\KP\Newpy\blb_ins.py", line 21, in insertBLOB cursor.execute(sqlite_insert_blob_query, data_tuple) MemoryError
Reply
#2
Why are you trying to store a Zip file in the database in the first place?
Reply
#3
I have already stored a .zip file as the first record. This is the second record I want to store.
Reply
#4
Blobs are one of a few sqlite limitations. it can't handle anything larger than 2GB (unless broken into chunks, and then reassembled after extraction). see: https://www.sqlite.org/limits.html

Another possibility that I'm not sure about (need to research) is that the insert may need enough system memory to hold blob in memory while writing. If so, there may be a way to reduce the buffer size (again need to research, as I'm thinking like a software engineer)
buran likes this post
Reply
#5
The .zip file is < 2GB. My doubt is that I inserted the first record successfully. I then closed the program and opened it again to insert the second record. Is it that the cursor remains on the first record and needs to be shifted to a new blank record before executing the insert querry?
Reply
#6
Memory is probably still being held after insert, until database is closed.
you can try to:
  1. Close cursor after insert
  2. Issue a vacuum statement after the search. (this will probably work, but may be restrictively slow.
Reply
#7
Thanks GOD BLESS YOU!!!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020