22

I want to insert image into a table like

 CREATE TABLE XX_SAMPLE(ID INT
                       ,IMAGE BLOB);

So can you help out form how to insert image into the above table.

tk_
  • 16,415
  • 8
  • 80
  • 90
Viru
  • 525
  • 2
  • 7
  • 15
  • 1
    Well, you can put it into a database (with modern versions of mySql), but it's better to upload the file to either your server, or someone elses (like imgur) and store the URL in the database. – Ben Dec 01 '16 at 01:00

9 Answers9

40

Please try below code

INSERT INTO xx_BLOB(ID,IMAGE) VALUES(1,LOAD_FILE('E:/Images/jack.jpg'));
Community
  • 1
  • 1
Madhav
  • 2,285
  • 3
  • 17
  • 16
9

You should use LOAD_FILE like so:

LOAD_FILE('/some/path/image.png')
Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
4

Step 1: open your mysql workbench application select table. choose image cell right click select "Open value in Editor" enter image description here

Step 2: click on the load button and choose image file enter image description here

Step 3:then click apply buttonenter image description here

Step 4: Then apply the query to save the image .Don't forgot image data type is "BLOB". Step 5: You can can check uploaded image enter image description here

Ganesan J
  • 539
  • 7
  • 12
1

You can try something like this..

CREATE TABLE 'sample'.'picture' ( 
'idpicture' INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, 
'caption' VARCHAR(45) NOT NULL, 
'img' LONGBLOB NOT NULL, 
 PRIMARY KEY('idpicture')) TYPE = InnoDB;

or refer to the following links for tutorials and sample, that might help you.

http://forums.mysql.com/read.php?20,17671,27914

http://www.hockinson.com/programmer-web-designer-denver-co-usa.php?s=47

Dalmarus
  • 101
  • 6
x'tian
  • 734
  • 2
  • 14
  • 40
  • 2
    Can you explain that further? How does using another table definition solve the given problem? Additionally, don't refer to external pages, put everything needed into the answer itself. Two of these links are dead – Nico Haase Jan 07 '20 at 10:14
0

If I use the following query,

INSERT INTO xx_BLOB(ID,IMAGE) 
VALUES(1,LOAD_FILE('E:/Images/xxx.png'));

Error: no such function: LOAD_FILE

Serjik
  • 10,543
  • 8
  • 61
  • 70
  • 1
    Dont use loadFile, add the value as a VARCHAR data type, once retrieved in the application layer the application code can be used to fetch the image referenced by this URL. – Samuel Owino Jun 05 '17 at 13:23
0

I have three answers to this question:

  1. It is against user experience UX best practice to use BLOB and CLOB data types in string and retrieving binary data from an SQL database thus it is advised that you use the technique that involves storing the URL for the image( or any Binary file in the database). This URL will help the user application to retrieve and use this binary file.

  2. Second the BLOB and CLOB data types are only available to a number of SQL versions thus functions such as LOAD_FILE or the datatypes themselves could miss in some versions.

  3. Third DON'T USE BLOB OR CLOB. Store the URL; let the user application access the binary file from a folder in the project directory.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Samuel Owino
  • 747
  • 1
  • 12
  • 24
0

I tried all above solution and fail, it just added a null file to the DB.

However, I was able to get it done by moving the image(fileName.jpg) file first in to below folder(in my case) C:\ProgramData\MySQL\MySQL Server 5.7\Uploads and then I executed below command and it works for me,

INSERT INTO xx_BLOB(ID,IMAGE) VALUES(1,LOAD_FILE('C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/fileName.jpg'));

Hope this helps.

tk_
  • 16,415
  • 8
  • 80
  • 90
0

The accepted answer is correct, but I came across so many cases where LOAD_FILE() returns NULL (like discussed in 24459149) that I normally have been better off, creating an import file with a hex representation:

docker run -d -p 3306:3306 -e MARIADB_ROOT_PASSWORD=test -e MARIADB_USER=test -e MARIADB_PASSWORD=test -e MARIADB_DATABASE=testdb mariadb:latest
echo "CREATE TABLE XX_SAMPLE(ID INT, IMAGE LONGBLOB)" |mysql -h 127.0.0.1 -u test --password=test testdb

echo -n "INSERT INTO XX_SAMPLE(ID,IMAGE) VALUES(1, 0x" >img_import.sql
cat /some/path/image.png |perl -ne "print unpack 'H*'" >> img_import.sql
echo ");" >> img_import.sql

mysql -h 127.0.0.1 -u test --password=test testdb <img_import.sql

Also Perl's unpack has been easier to use, compared to xxd or hexdump that are not always installed and need special flags to get that long string of hex values without breaks, that the mysql client expects.

UPDATE:

  • Changed the quotes as @Terai proposed. (Seems to be a quoting issue that doesn't occur in my ZSH.)
  • Added other DDL for the example table, as the original DDL above uses a BLOB. With normal size images you will need MEDIUMBLOB or even LONGBLOB.
  • Added a sample docker container to complete the example.
dheuvels
  • 91
  • 5
  • the second line produce an error : syntax error at -e line 1, at EOF Execution of -e aborted due to compilation errors. – Terai Mar 15 '23 at 07:56
  • You need to change 'print unpack "H*"' by "print unpack 'H*'" – Terai Mar 15 '23 at 09:43
-1

This is on mysql workbench -- give the image file path:

INSERT INTO XX_SAMPLE(id,image) VALUES(3,'/home/ganesan-pc/Documents/aios_database/confe.jpg');
Ganesan J
  • 539
  • 7
  • 12
  • Can you explain that further? According to the table definition, the second value should not be a string containing the file path, but the content of that file - that's why other answers already contain this – Nico Haase Jan 07 '20 at 10:13