How to insert images into a MySQL table with BLOB type
This article explains how to insert images into a MySQL table with BLOB
type.
Some projects require a large string or block of binary data to be stored in a database. A BLOB (Binary Large Object) is a data type that can be used to store a large collection of binary data in a database table. A CLOB (Character Large Object) is a data type that can be used to store a large collection of character data in a database table.
For example, a digital file containing a picture, video, or a song can be stored in a database using a BLOB, or a plain text file can be stored in a database using a CLOB. This article explains how to insert images into a Mysql table with BLOB type.
The example can be adapted, with minor changes, to use the CLOB type or another database.
Prerequisites
- This example uses a MySQL table, named blobdemo, with two fields:
Id and Picture. The table definition is
as follows:
- A specific folder, in this example "C:/Talend/images", stores the pictures that will be inserted into the blobdemo table.
Creating a user routine
Procedure
Creating a Job to insert BLOB data into a MySQL table
Create a Job to iterate over the picture files in the
"C:/Talend/images" folder and insert each one into the
blobdemo table.
- The tFileList iterates all pictures in the "C:/Talend/images" folder.
- The tFixedFlowInput gets the current file path and outputs it.
- The tMap calls the routine to read the files as byte array.
- The tMysqlOutput inserts the BLOB data into the target table.
Set up the Job
Procedure
- Create a Standard Job and enter its name, in this example Insertblobdatatodatabase.
- Drag and drop the following components to the design workspace: tFileList, tFixedFlowInput, tMap and tMysqlOutput.
- Connect the tFileList and the tFixedFlowInput using an Iterate link.
- Connect the tFixedFlowInput to the tMap and the tMap to the tMysqlOutput using links.
Configure the data input
Procedure
Configure the tMap component to call the user routine
Before you begin
- You have created a user routine.
Procedure
Insert the BLOB data into the target table
Before you begin
- You have created a MySQL table.
Procedure
Executing the Job
Procedure
- Press Ctrl+S to save your Job.
- Press F6 to execute the Job.
- Query the blobdemo table to verify that the pictures were inserted successfully.