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

Introduction and usage examples of ref and $refs in Vue

Preface In JavaScript, you need to use document.q...

How to set the default value of a MySQL field

Table of contents Preface: 1. Default value relat...

Detailed installation and use of RocketMQ in Docker

To search for RocketMQ images, you can search on ...

VUE Getting Started Learning Event Handling

Table of contents 1. Function Binding 2. With par...

View the dependent libraries of so or executable programs under linux

View the dependent libraries of so or executable ...

Detailed steps for QT to connect to MYSQL database

The first step is to add the corresponding databa...

Detailed explanation of meta tags and usage in html

I won’t waste any more time talking nonsense, let...

Three JavaScript methods to solve the Joseph ring problem

Table of contents Overview Problem Description Ci...

The difference and usage of Vue2 and Vue3 brother component communication bus

Table of contents vue2.x vue3.x tiny-emitter plug...