将文件内容插入 MySQL 表的列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3101021/
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 16:24:39  来源:igfitidea点击:

Insert file contents into MySQL table's column

mysqlfileinsert

提问by Martin Tóth

I know there's the LOAD DATA INFILEstatement, which allows me to INSERT structured data into a table.

我知道有一个LOAD DATA INFILE语句,它允许我将结构化数据插入到表中。

What I'm curious about, is whether it is possible to INSERT contents of a file into single column. So, is something like this:

我很好奇的是,是否可以将文件的内容插入到单列中。所以,是这样的:

INSERT INTO my_table (stamp, what) VALUES (NOW(), LOAD DATA INFILE 'my_file');

possible?

可能的?

回答by Daniel Vassallo

Yes it's possible. You can use the LOAD_FILE()function:

是的,这是可能的。您可以使用该LOAD_FILE()功能:

CREATE TABLE my_table (stamp datetime, what text);

INSERT INTO my_table (stamp, what) VALUES (NOW(), LOAD_FILE('/tmp/my_file.txt'));

You'll have to make sure that the file is readable by MySQL, and that your MySQL user has the FILEprivilege. This is the same privilege required for LOAD DATA INFILE.

您必须确保 MySQL 可以读取该文件,并且您的 MySQL 用户具有该FILE权限。这与LOAD DATA INFILE.

回答by DrColossos

edit: I understood you wrong, sorry, Daniel Vassallo has the right answer. May this is useful anyway

编辑:我理解你错了,对不起,丹尼尔瓦萨洛有正确的答案。无论如何这可能有用

I think you are looking for BLOBor TEXT, see docs. If you want to store file date inside the datebase, these are what you are possibly looking for

我认为您正在寻找BLOBor TEXT,请参阅文档。如果您想将文件日期存储在数据库中,这些就是您可能正在寻找的