V
V
Vladimir Korshunov2021-01-29 17:09:45
Python
Vladimir Korshunov, 2021-01-29 17:09:45

How to add file to BLOB in sqlite(python)?

I read somewhere that BLOB files are stored simply in hex. Accordingly, I converted the file I needed to hex, tried to insert it into a BLOB and got an error:

file = open(f'/Users/vladimirkorsunov/Downloads/bot_pdf/{pdf_file_id}.pdf', 'rb')
    bin_file = file.read()
    hex_file = binascii.hexlify(bin_file)
    conn = sqlite3.connect("orders_list.db")
    c = conn.cursor()
    c.execute(f"INSERT INTO orders (student_id, subject, pdf_file, exercises, price) VALUES ({student_id}, '{subject}', {hex_file}, '{exercises}', {price})")
    conn.commit()


I saw someone using hexdump, but I didn't understand what's what. Here is one example:
(printf "INSERT INTO test(id, name, blob_field) VALUES(NULL, 'Bob', 0x" ; \
 hexdump -v -e '/1 "%02x"' 001.png ; printf ");" ) | sqlite3 my.db

As I understood from the documentation, -e sets the string format, but why is it the way it is in the example? What does this even mean? In any case, I didn’t find any examples in python at all, so I can’t even try to use it,

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2021-01-29
@GashhLab

Ok Google, "python sqlite insert blob"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question