I am writing a bot in Python using sqlite. I want to hide different things in the database. The database supports image storage in BLOB cells. How to throw there, for example * .pdf file and how to get it from there then.

Python 3.5 is used

  • 0_o))) What other headers? And where does it register? You want to store a large text - before placing it in the database, compress it using the LZMA, or PPMd algorithm, and write the resulting binary data in the BLOB. I do not understand the essence of the issue. - Majestio
  • four
    The DB "does not register anything" anywhere. What she was given is kept and returned. No initiative without an order. If it doesn’t return what was deposited, either ask the wrong way or take an unnecessary initiative (or contain errors) the means that it receives / displays. - Akina
  • We are talking about the file headers, their descriptors. BLOB is a byte code, you can shove anything there. Using DB Browser for Sqlite, for testing purposes, I uploaded a text file to the database, but when it was uploaded, he added the file header (descriptor) a second time and the text became unreadable. And I may want to add * .xlsx or * .docx files to the database. I am interested in how to save a file in DB using Python, and how to get it out correctly from there - 6NGY30E
  • What is, in your understanding, the "file descriptor"? - Pavel Mayorov
  • @PavelMayorov open-file.ru/articles/file-header but I'm not talking about that. I need to write the file in byte-by-byte into a table and byte-by-bye extract from there. No distortion. Most likely, the descriptor theme is associated with the DB Browser for Sqlite, and not with the sqlite itself. In any case, I don’t know how to write a byte-byte file to a table using python - 6NGY30E

1 answer 1

import sqlite3 as lite import sys con = lite.connect("test.db") cur = con.cursor() #Загрузка файла в БД file_input = open("blank.pdf", "rb") file = file_input.read() file_input.close() binary = lite.Binary(file) cur.execute("INSERT INTO files(file) VALUES (?)", (binary,) ) con.commit() #Выгрузка файла из БД cur.execute("SELECT file FROM files LIMIT 1") data = cur.fetchone()[0] file_output = open("blank2.pdf","wb") file_output.write(data) file_output.close() con.close() 
  • one
    You should not use import sqlite3 as lite — this degrades the readability of the code without a reason. - jfs