如何在Python中将数字文件存储在数据库中

Databases like MySQL and PostgreSQL are all ideal for storing tables with numeric and text data. However, can you imagine them storing your favorite song or a video in one of their cells?

MySQLPostgreSQL这样的数据库都是存储带有数字和文本数据的表的理想选择。 但是,您能想象他们将喜欢的歌曲或视频存储在其中一个单元中吗?

I had to learn the technique for storing files in a database while I was doing a personal project. I wanted to upload a large number of audio files in a remote database and had no idea how. If you were like me, you probably would not have the tiniest clue of how a file would like in a database.

在执行个人项目时,我必须学习将文件存储在数据库中的技术。 我想在远程数据库中上传大量音频文件,却不知道如何上传。 如果您像我一样,可能就不会知道文件在数据库中的样子了。

先决条件 (Prerequisites)

Before we move on to the actual code, there are a few things you need to know. To insert any file into a database table, you need to know the table name and a little bit of SQL. Also, the column in which you want to store your files must be able to hold binary data.

在继续学习实际代码之前,您需要了解一些事情。 要将任何文件插入数据库表,您需要知道表名和一些SQL 。 另外,您要在其中存储文件的列必须能够保存二进制数据。

The code also changes according to what kind of database you use. There are several dialects of databases such as MySQL, PostgreSQL, SQLite, and so on. In Python, there are different packages for connecting to different databases. But the code is pretty much the same because of the Python Database API Specification which ensures that all packages that handle databases in Python must follow the same code syntax.

该代码还会根据您使用的数据库类型而变化。 数据库有几种方言,例如MySQLPostgreSQLSQLite等。 在Python中 ,有用于连接到不同数据库的不同软件包。 但是由于Python Database API Specification(Python数据库API规范)的代码几乎相同,该规范可确保使用Python处理数据库的所有程序包必须遵循相同的代码语法。

In this article, I will be using an SQLite database as an example, but if you want to use some other, I have written a detailed article on working with the remaining database dialects in Python:

在本文中,我将以一个SQLite数据库为例,但是如果您要使用其他数据库,我已经写了一篇详细的文章,介绍如何在Python中使用其余的数据库方言:

文件如何在表格单元格中表示 (How files are represented in a table cell)

In most databases, including SQLite, the column that stores the files must have a BLOB data type. BLOB stands for Binary Large Object.

在包括SQLite在内的大多数数据库中,存储文件的列必须具有BLOB数据类型。 BLOB

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值