Skip to main content

file_read

1. FILE_READ Function

The FILE_READ function in H2 database is used to read the content of a file and return it as a string.

2. Syntax

The syntax for the FILE_READ function in H2 database is as follows:

FILE_READ(filename)

Arguments

  • filename: The name or path of the file to be read. It should be a string value specifying the file location.

Return

  • The FILE_READ function returns the content of the file as a string.

3. Notes

  • The FILE_READ function in H2 database reads the content of a file from the file system. It requires the necessary file system permissions to access the file.
  • The file should be accessible to the H2 database server. The path can be relative or absolute, depending on the file's location.
  • If the file does not exist or cannot be read, the FILE_READ function will return NULL.
  • Ensure that the file is closed properly after reading to avoid resource leaks.

4. Examples

Here are a few examples demonstrating the usage of the FILE_READ function in H2 database:

Example 1 - Reading the content of a file:

SELECT FILE_READ('/path/to/file.txt') AS file_content;

Output:

file_content
--------------------------
This is the file content.
It has multiple lines.

Example 2 - Reading the content of a file specified in a column:

CREATE TABLE files (
id INT PRIMARY KEY,
filepath VARCHAR(255)
);

INSERT INTO files VALUES (1, '/path/to/file1.txt'), (2, '/path/to/file2.txt');

SELECT id, filepath, FILE_READ(filepath) AS file_content
FROM files;

Output:

id |       filepath        |         file_content
---+-----------------------+-------------------------------
1 | /path/to/file1.txt | This is the content of file 1.
2 | /path/to/file2.txt | This is the content of file 2.