Python Forum
[Solved] Retrieving a pdf from sqlite3
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved] Retrieving a pdf from sqlite3
#1
What I am trying to do
pdf files are stored in my sqlite3 database as blobs.
I want to select one specific pdf file and save it as a file.

That's my python error message:
sqlite3.OperationalError: Could not decode to UTF-8 column 'dokument' with text '%PDF-1.2

What's my python code
I tried a lot, also copied and paste a lot from the internet. I assume it's less an issue of the code, but more that I forget something on a higher level (e.g. sqlite3 configuration)?
Anyhow: the very latest code I work with:

def readBlobData_and_safe_as_file(path_to_db, select_query):
    con = sqlite3.connect(path_to_db)
    cur = con.cursor()

    with open("/home/Datei.pdf", "wb") as output_file:
        cur.execute(select_query)
        blobdat = cur.fetchone()
        output_file.write(blobdat[0])
        cur.close()
        con.close()
After one week of try-and-error, you can't believe how grateful I would be for support. Millions thanks in advance.
(remark: I am not a professionel programer, I do this for fun and as a hobby).
Reply
#2
Have you tried the solutions from this page? for example setting conn.text_factory = bytes
Reply
#3
(Mar-12-2022, 07:28 AM)Gribouillis Wrote: Have you tried the solutions from this page? for example setting conn.text_factory = bytes

Holly sh****, this single addiional line (con.text_factory = bytes) solved it. Thank you, thank you thank you!!! You are my hero! Great!!!
Reply
#4
Why do you want to store the PDFs in the database in the first place? Wouldn't it make more sense to save the files and store the paths to them in the database instead?
Reply
#5
Files are so last century.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  KeyError while retrieving ESPN data john317ab 2 769 Nov-29-2023, 09:07 PM
Last Post: john317ab
  .get() not retrieving value? Sedos101 2 534 Aug-25-2023, 11:48 AM
Last Post: deanhystad
  Retrieving a column from a data set using a function Bayle 6 2,290 Oct-06-2021, 08:52 PM
Last Post: Bayle
  Retrieving Cookies whois1230 2 2,140 Nov-21-2020, 12:01 PM
Last Post: snippsat
  Problem: Retrieving Form data PythonDev 3 3,025 Oct-16-2020, 02:09 AM
Last Post: PythonDev
  Retrieving dictionary keys within with another dictionay bazcurtis 8 2,728 Oct-29-2019, 10:06 PM
Last Post: bazcurtis
  Retrieving items from JSON bazcurtis 12 4,953 Oct-27-2019, 05:18 PM
Last Post: bazcurtis
  Trouble retrieving dictionary from mysql.connector cursor swechsler 2 2,993 Sep-17-2019, 05:21 PM
Last Post: swechsler
  retrieving pvalue from statsmodels results Staph 4 2,977 Jul-18-2019, 03:27 PM
Last Post: Gribouillis
  PRAW and PyQt: Immense slowdown when retrieving Reddit posts and adding them to GUI codebro 2 3,177 Dec-30-2018, 01:19 AM
Last Post: codebro

Forum Jump:

User Panel Messages

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