当前位置: 移动技术网 > IT编程>数据库>Mysql > MySQL使用LOAD_FILE()函数方法总结

MySQL使用LOAD_FILE()函数方法总结

2019年07月19日  | 移动技术网IT编程  | 我要评论

在mysql中,load_file()函数读取一个文件并将其内容作为字符串返回。

语法

load_file(file_name)

其中file_name是文件的完整路径。

下面是我从一个文件中选择内容的示例:

select load_file('/data/test.txt') as result;

结果:

+------------------------------------------+

| result |

+------------------------------------------+

| this text is all that the file contains! |

+------------------------------------------+

一个数据库的例子

下面是一个将文件内容插入数据库时查询的示例:

insert into mytable (fileid, userid, myblobcolumn)

values (1, 20, load_file('/data/test.txt'));

在本例中,列myblobcolumn有一个blob数据类型(允许它存储二进制数据)。

现在它在数据库中,我们可以选择它:

select myblobcolumn

from mytable

where userid = 20;

结果:

+------------------------------------------+

| myblobcolumn |

+------------------------------------------+

| this text is all that the file contains! |

+------------------------------------------+

如果文件不存在,返回null:

select load_file('/data/oops.txt') as result;

结果:

+--------+

| result |

+--------+

| null |

+--------+

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网