Summary of MySQL using LOAD_FILE of function method
- 2021-11-24 03:13:21
- OfStack
In MySQL, the LOAD_FILE () function reads a file and returns its contents as a string.
Grammar
LOAD_FILE(file_name)
Where file_name is the full path of the file.
The following is an example of what I selected from 1 file:
SELECT LOAD_FILE('/data/test.txt') AS Result;
Results:
+------------------------------------------+
| Result |
+------------------------------------------+
| This text is all that the file contains! |
+------------------------------------------+
1 example of a database
The following is an example of a query when inserting file contents into the database:
INSERT INTO MyTable (FileId, UserId, MyBlobColumn)
VALUES (1, 20, LOAD_FILE('/data/test.txt'));
In this example, the column MyBlobColumn has one BLOB data type (allowing it to store binary data).
Now that it is in the database, we can select it:
SELECT MyBlobColumn
FROM MyTable
WHERE UserId = 20;
Results:
+------------------------------------------+
| MyBlobColumn |
+------------------------------------------+
| This text is all that the file contains! |
+------------------------------------------+
If the file does not exist, return NULL:
SELECT LOAD_FILE('/data/oops.txt') AS Result;
Results:
+--------+
| Result |
+--------+
| NULL |
+--------+