如何存储 .txt 文件 MySQL 数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5278759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:04:28  来源:igfitidea点击:

How to store .txt files MySQL database?

mysqldatabasedatabase-design

提问by Rida Shamasneh

Can I store data files (e.g. txt files) to the MySql server? If I can, how to store them?

我可以将数据文件(例如 txt 文件)存储到 MySql 服务器吗?如果可以,如何保存?

回答by Mark Byers

You can use LOAD DATA INFILEto read the contents of a file and store it in a table in the database in a structured format.

您可以使用LOAD DATA INFILE读取文件的内容并将其以结构化格式存储在数据库中的表中。

This can be considerably faster than reading and parsing the file on the client and then using multiple INSERT statements.

这比在客户端读取和解析文件然后使用多个 INSERT 语句要快得多。

Example:

例子:

LOAD DATA INFILE 'data.txt' INTO TABLE db2.my_table;

回答by Abe Miessler

Yes you can, but you would probably be better off storing them on the file system and storing a path to the file in the DB.

是的,您可以,但您最好将它们存储在文件系统中并将文件的路径存储在数据库中。

There are a few SO Posts that discuss this:

有一些 SO 帖子讨论了这个问题:

回答by Michael

Sure you can. I would suggest reading the data from your files and then saving it in your database, i would say in a text field.

你当然可以。我建议从您的文件中读取数据,然后将其保存在您的数据库中,我会在文本字段中说。

You can use something like thisto get the file's content:

您可以使用像这样得到的文件的内容:

$file = file_get_contents('./yourfile.txt');

Then insert it

然后插入

Insert into myTable VALUES (mytextfile = $file)

回答by Babak Naffas

How much text are we talking about here? If there's not that much text, you can store just the content of the file in the database.

我们在这里谈论多少文本?如果没有那么多文本,您可以只将文件的内容存储在数据库中。

You can also store the file itself as a BLOB. http://dev.mysql.com/doc/refman/5.5/en/blob.html

您还可以将文件本身存储为 BLOB。http://dev.mysql.com/doc/refman/5.5/en/blob.html

If you'll be dealing with many files, you'll probably be better off storing the files on your sever with the file path in the database.

如果您要处理许多文件,最好将文件与数据库中的文件路径一起存储在服务器上。