Failed attempts to retrieve the binary large object (BLOB) from SQLite using Flask SQLAlchemy results in a null

I'm facing an issue while attempting to retrieve a BLOB (LargeBinary) object from SQLite using Flask-SQLAlchemy. The error message I'm encountering is:

TypeError: must be string or read-only buffer, not None
.

This is the code snippet that I am working with:

@app.route('/csv/<int:job_id>', methods=['GET'])
def get_csv(job_id):
    """ Function to fetch the compressed CSV from the database """
    job = db.session.query(Job).filter_by(id=job_id).first()
    csv = decompress(job.compressed_csv)
    sio = StringIO()
    sio.write(csv)
    sio.seek(0)
    return send_file(sio,
                     attachment_filename=
                     "{}_{}.txt".format(job_id, time.strftime("%Y%m%d%H%M%S")),
                     as_attachment=True)

Below is my SQLAlchemy model definition:

class Job(db.Model):
    """ Represents a Job in SQLAlchemy """
    id = db.Column(db.Integer, primary_key=True)
    list_type_id = db.Column(db.Integer, db.ForeignKey('list_type.id'),
                             nullable=False)
    list_type = db.relationship('ListType',
                                backref=db.backref('jobs', lazy='dynamic'))
    record_count = db.Column(db.Integer, nullable=False)
    status = db.Column(db.Integer, nullable=False)
    sf_job_id = db.Column(db.Integer, nullable=True)
    created_at = db.Column(db.DateTime, nullable=False)
    compressed_csv = db.Column(db.LargeBinary)

    def __init__(self, list_type_id, record_count=0, status=0, sf_job_id=0,
                 csv=None, created_at=datetime.utcnow()):
        self.list_type_id = list_type_id
        self.created_at = created_at
        self.record_count = record_count
        self.status = status
        self.sf_job_id = sf_job_id
        self.compressed_csv = csv

    def __repr__(self):
        return '<Job {}>'.format(self.id)

Here's the schema for the database tables:

CREATE TABLE alembic_version (
    version_num VARCHAR(32) NOT NULL
);
CREATE TABLE job (
    id INTEGER NOT NULL, 
    list_type_id INTEGER NOT NULL, 
    record_count INTEGER NOT NULL, 
    status INTEGER NOT NULL, 
    sf_job_id INTEGER NOT NULL, 
    created_at DATETIME NOT NULL, 
    compressed_csv BLOB, 
    PRIMARY KEY (id), 
    FOREIGN KEY(list_type_id) REFERENCES list_type (id)
);
CREATE TABLE list_type (
    id INTEGER NOT NULL, 
    name VARCHAR(80) NOT NULL, 
    PRIMARY KEY (id), 
    UNIQUE (name)
);

Answer №1

After some investigation, I discovered that the issue was caused by my use of csv = buffer(compress(csv)) when inserting the data. This mistake led to NULL being stored for that particular field. Fortunately, removing the buffer function fixed the problem.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Python and Selenium Troubleshooting: Download Error Issues

I am facing an issue with my script where it is giving a "Failed - Download Error" message when trying to download a PDF file. However, the manual process works perfectly fine. from selenium import webdriver from selenium.webdriver.common.keys import Keys ...

Seal the h5py data file that is currently open

Within our laboratory, data is stored in hdf5 files using the Python package h5py. When starting an experiment, we initiate a new hdf5 file and continuously add arrays of data to it. However, if an experiment fails or is interrupted, the file may not be p ...

Tips for merging a 2D array with a single value repeated multiple times to fill any gaps

I have x and y numpy arrays: import numpy as np np.random.seed(1) x = np.random.rand(3, 2) y = np.random.rand(1) My goal is to merge the two arrays in a way that transforms the shape of x to (x.shape[0] by x.shape[1] + 1). Since y is a scalar, I need it ...

Executing tasks concurrently in Snakemake rule

Apologies if this question seems basic, but I'm still grappling with the complexities of Snakemake. I have a folder full of files that I need to process in parallel using a rule (i.e. running the same script on different input files simultaneously). ...

Python: Flattening and Extracting specific JSON segments

I've got an input JSON that has the following structure: > {"payment": {"payment_id": "AA340", "payment_amt": "20", "chk_nr": "321749", "clm_list": {"dtl": [{"clm_id": "1A2345", "name": "John", adj:{"adj_id":"W123","adj_cd":"45"}}, {"clm_id": "999 ...

Python Selenium script that includes printing to the command line in the middle

first_run = res_loop(url, tr) if first_run > 0: xml_input = first_run else: loop_decision = input("Do you want to run the script until a time slot is found?\n") if loop_decision[0].upper() == 'Y': loop_outpu ...

I've recently begun my journey with Python and am currently working through a tutorial. I've noticed that even though I'm implementing the tutorial's code exactly as instructed, it's

from selenium import webdriver PATH = "C:\Program Files\Driver\chromedriver-win32\chromedriver-win32" driver = webdriver.Chrome(PATH) driver.get("https://example.com") however, there is an occurrence of error " ...

Learn how to retrieve values from a .json file in real-time and then perform comparisons with user input using Python

I have a JSON file structured like this: [ { "name": { "common": "Aruba", "official": "Aruba", "native": { "nld": { "official ...

I encountered a problem extracting title URLs with Python during web scraping

I have encountered an issue while trying to scrape title URLs using my code. Can someone please help me troubleshoot it? Here is the code snippet: import requests from bs4 import BeautifulSoup # import pandas as pd # import pandas as pd import csv def ...

I encountered an issue where the link within the container was unclickable

Hello everyone, I appreciate you taking the time to read my question. Recently, I attempted to streamline some of my tasks by automating them. In doing so, I used an xpath finder tool to locate the Add button path, which resulted in three different links. ...

Using Flask to pass variable data from one route to another in Python using the `url

I am facing an issue with sending a variable value to Python from Flask HTML/JS via url_for(). Here's my Python code: @app.route('/video_feed/<device>') def video_feed(device): # return the response generated along with the speci ...

Error occurs when Django MultipleChoiceField options selected by users are not valid choices

I need help creating a form that allows users to select multiple users from a specific group. However, I encountered an error stating that the 'User' object does not support indexing when trying to use the list of users as options. The form is p ...

After installing MSYS2, pip is not found in the path and shows a command not found error

I just completed the installation of pip on MSYS2 by executing the command below within the MSYS2 shell: pacman -S mingw-w64-x86_64-python-pip (web page for the package: ) Upon successful installation, my attempt to run pip in the MSYS2 shell returned an ...

Selenium and TikTok incorporate a cookie consent button

Having an issue with my code where I can't click on the 'Accept Cookies' button on a TikTok page. Each time I run it, I get a Timeoutexception error. from selenium import webdriver import time from selenium.webdriver.chrome.options import Op ...

Determining User Affiliation in Django: Steps to Verify if Users Registered under Me Have New Registrations

Currently, I am working on developing a referral system logic in Django. The referral system is functioning properly at the moment, but I have a new requirement to implement. When I refer both "user 2" and "user 3", the system records that I have referred ...

Using Pandas in Python to filter and group data based on specific criteria

Consider a dataset that contains both categorical and numerical columns, such as a salary dataset. The columns can be categorized as follows: ['job', 'country_origin', 'age', 'salary', 'degree','marit ...

Exploring the World of Search Trends with Google Trends

Is there a way to extract or download Google Trends Series Data by category and/or subcategory using Python? I came across this list of categories on the following link: https://github.com/pat310/google-trends-api/wiki/Google-Trends-Categories The list pr ...

Progress Bar in wxPython is an essential tool for tracking

Instead of using the wx.ProgressDialog, I am looking for a control that only includes the progress bar so I can add extra features like a pause button and processing information to my own dialog box. I could design something myself, but in order for the p ...

How can recursive data be displayed in a template?

I am working with a model in Django that has a ForeignKey pointing to itself, and I need to display all the data from the database using lists and sublists: Below is my model definition: class Place(models.Model) name = models.CharField(max_length=1 ...

Python and SQLAlchemy tutorial: Establishing a connection to a MySQL server with Cleartext Authentication enabled

Seeking assistance in accessing a MySQL server that only allows mysql_clear_password setup. I have successfully connected using the --enable-cleartext-plugin option through the command line: >> mysql --port NNN -u my_user -h DB.host.com --enable-cle ...