Which SQLAlchemy column type should be used for binary data?

后端 未结 2 2065
情书的邮戳
情书的邮戳 2020-12-12 01:38

I want to store audio files in my database. I know, for example, that strings would use db.String, integers db.Integer, but not what audio data wo

2条回答
  •  没有蜡笔的小新
    2020-12-12 01:54

    When storing binary data, use the LargeBinary type. Despite its name, it is appropriate for any sized binary data.

    data = db.Column(db.LargeBinary)
    

    Read the data from the uploaded file in your Flask view.

    audio.data = request.files['data'].read()
    

    Rather than storing the data in the database, it's typically better to store files in the filesystem, and store the path to the file in the database.

    Since you're presumably serving these files, not just storing them, it is much more efficient to serve the files from the filesystem. See this answer for more discussion on serving data from the database.

提交回复
热议问题