Home >Database >Mysql Tutorial >How do you efficiently store and retrieve BLOB and CLOB files in MySQL?

How do you efficiently store and retrieve BLOB and CLOB files in MySQL?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-12 13:59:02514browse

How do you efficiently store and retrieve BLOB and CLOB files in MySQL?

Storing BLOB and CLOB Files in MySQL

Inserting images and various file formats (.docx/.doc, .pptx/.ppt, .pdf) into a database can be a common requirement for software applications. MySQL provides a convenient way to store these binary large objects (BLOBs) and character large objects (CLOBs) within tables.

There are two primary methods to accomplish this insertion:

1. LOAD_FILE Function:

This function allows you to insert a file directly into a BLOB field. Simply reference the file path like so:

INSERT INTO table1 VALUES(1, LOAD_FILE('data.png'));

2. Hex String Insertion:

Alternatively, you can convert the file into a hexadecimal string and insert it into a BLOB or CLOB field. This method might be more performant for large files. Here's an example:

INSERT INTO table1 VALUES 
  (1, x'89504E470D0A1A0A0000000D494844520000001000000010080200000090916836000000017352474200AECE1CE90000000467414D410000B18F0BFC6105000000097048597300000EC300000EC301C76FA8640000001E49444154384F6350DAE843126220493550F1A80662426C349406472801006AC91F1040F796BD0000000049454E44AE426082');

Both methods can be used to efficiently insert and retrieve BLOB and CLOB files in MySQL, allowing you to store and manage various file formats alongside your other data.

The above is the detailed content of How do you efficiently store and retrieve BLOB and CLOB files in MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn