Summary of MySQL LOAD_FILE() function method

Summary of MySQL LOAD_FILE() function method

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 to the file.

Here is an example of me selecting content from a file:

SELECT LOAD_FILE('/data/test.txt') AS Result;

result:

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

| Result |

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

| This text is all that the file contains! |

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

An example of a database

Here is an example of a query that inserts the contents of a file into a database:

INSERT INTO MyTable (FileId, UserId, MyBlobColumn)

VALUES (1, 20, LOAD_FILE('/data/test.txt'));

In this example, the column MyBlobColumn has a 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;

result:

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

|MyBlobColumn|

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

| This text is all that the file contains! |

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

If the file does not exist, returns NULL:

SELECT LOAD_FILE('/data/oops.txt') AS Result;

result:

+--------+

| Result |

+--------+

| NULL |

+--------+

You may also be interested in:
  • Example of using go xorm to operate mysql
  • Mysql classic high-level/command line operation (quick) (recommended)
  • A brief discussion on the differences between the three major databases: Mysql, SqlServer, and Oracle
  • The use of mysql unique key in query and related issues
  • How to implement scheduled backup of MySQL database
  • Import backup between mysql database and oracle database
  • Detailed explanation of how a SQL statement is executed in MySQL
  • Detailed explanation of mysql download and installation process
  • MySQL 8.0.15 download and installation detailed tutorial is a must for novices!
  • Detailed explanation of how to migrate a MySQL database to another machine

<<:  JD Vue3 component library supports the detailed process of mini program development

>>:  Linux uses iftop to monitor network card traffic in real time

Recommend

Rounding operation of datetime field in MySQL

Table of contents Preface 1. Background 2. Simula...

Detailed explanation of Truncate usage in MySQL

Preface: When we want to clear a table, we often ...

Use elasticsearch to delete index data regularly

1. Sometimes we use ES Due to limited resources o...

Implementation of deploying war package project using Docker

To deploy war with Docker, you must use a contain...

XHTML Web Page Tutorial

<br />This article is mainly to let beginner...

Detailed explanation of grep and egrep commands in Linux

rep / egrep Syntax: grep [-cinvABC] 'word'...

Example of converting spark rdd to dataframe and writing it into mysql

Dataframe is a new API introduced in Spark 1.3.0,...

Vue encapsulates the public function method of exporting Excel data

vue+element UI encapsulates a public function to ...

How to use JavaScript strategy pattern to validate forms

Table of contents Overview Form validation withou...

Pure CSS3 mind map style example

Mind Map He probably looks like this: Most of the...

A designer complains about Hammer's official website again

Last year, the open letter was a huge hit, even a...

MySQL 8.0.22 installation and configuration graphic tutorial

MySQL8.0.22 installation and configuration (super...

A possible bug when MySQL executes the sum function on the window function

When using MySql's window function to collect...

How to use provide to implement state management in Vue3

Table of contents Preface How to implement Vuex f...

MySQL big data query optimization experience sharing (recommended)

Serious MySQL optimization! If the amount of MySQ...