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

Vue component encapsulates sample code for uploading pictures and videos

First download the dependencies: cnpm i -S vue-uu...

Detailed explanation of JavaScript axios installation and packaging case

1. Download the axios plugin cnpm install axios -...

Quickly install MySQL5.7 compressed package on Windows

This article shares with you how to install the M...

HTML+CSS to create heartbeat special effects

Today we are going to create a simple heartbeat e...

Docker+nextcloud to build a personal cloud storage system

1. Docker installation and startup yum install ep...

JS ES new features: Introduction to extension operators

1. Spread Operator The spread operator is three d...

Problems and experiences encountered in web development

<br />The following are the problems I encou...

How to support Webdings fonts in Firefox

Firefox, Opera and other browsers do not support W...

Nginx installation error solution

1. Unzip nginx-1.8.1.tar.gz 2. Unzip fastdfs-ngin...

Detailed explanation of uniapp painless token refresh method

When the front-end requests the interface, it is ...

How to display TIF format images in browser

The browser displays TIF format images Copy code T...

Vue implements graphic verification code

This article example shares the specific code of ...

Solution to Apache cross-domain resource access error

In many cases, large and medium-sized websites wi...

Analyze how to automatically generate Vue component documentation

Table of contents 1. Current situation 2. Communi...

Detailed graphic explanation of mysql query control statements

mysql query control statements Field deduplicatio...