0

I am trying to upload some file to my Sqlite Database.However, I am getting this error when I select my file (.xlsx) and I click to submit form.

sqlalchemy.exc.StatementError: (builtins.TypeError) memoryview: a bytes-like object is required, not 'str'
[SQL: INSERT INTO "FileData" (id, file) VALUES (?, ?)]
[parameters: [{'file': 'file.xlsx', 'id': 33065}]]

I developed a little Flask app which receives as input this Blob file through an html form:

{% extends "base.html" %} {% block title %} Home {% endblock %} {% block content%}
<html>
<head>
<title>Upload File to SQLITE</title>
</head>
<body style='background-color: #D1D1D1;'>
<div class='div-primary'>
    <h1>Upload a File</h1>
    <form action = '/' method = 'POST' enctype = 'multipart/form-data'>
    <div class = 'row'>
        <div class = 'frm-fld-label'>Select a file from your computer</div>
        <div class = 'frm-fld-input'><input type = 'file' class = 'div-input-file' name = 'file' id = 'file'></div>
    </div>
    <div class = 'row'>
        <div class = 'frm-fld-btn-save'><button type = 'submit' class = 'frm-bnt-save-file'>Upload Your File</button></div>
    </div>
    </form>
    {% endblock %}
</div>
</body>
</html>

The flask part:

@login_required

@views.route("/", methods=["GET", "POST"])
def home():
    random_file_id=random.randint(100000)
    if request.method=='POST':
        if random_file_id:
            file_uploaded=request.files['file']
            file_uploaded=secure_filename(file_uploaded.filename)               
            new_file=FileData(id=random_file_id,file=file_uploaded)
            db.session.add(new_file)
            db.session.commit()

        flash("File uploaded successfully", category='success')
    
    

    return render_template('home.html', user=current_user)

The data class definition :

class FileData(db.Model, UserMixin):
    __tablename__='FileData'
    id=db.Column(db.Integer, primary_key=True)
    file=db.Column(db.BLOB)

Is db.BLOB the correct definition for this type of file?

Lash
  • 333
  • 1
  • 2
  • 14

0 Answers0