How to insert images in blob in mysql table using only sql syntax (without PHP)?

蓝咒 提交于 2019-12-06 04:32:44

问题


  1. Hi,I am new to SQL and I wanted to store images in the database.I already created a column with blob data type and tried to execute the following statement as given here
INSERT INTO `abc`
    (`img`)
    SELECT  
        BulkColumn FROM OPENROWSET(
            Bulk 'C:\Users\adity\Desktop\New folder\a.png', SINGLE_BLOB) AS BLOB

which gives error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '( Bulk C:\Users\name\Desktop\New folder\a.png, SINGLE_BLOB) AS BLOB' at line 4

I also tried following code as given here

insert into table `abc`(`img`) values('C:\Users\name\Desktop\New folder\an.jpg') where id=1;

which gives the error

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table abc(img) values('C:\Users\adity\Desktop\New folder\an.jpg') where id=1' at line 1

So please suggest me how to store images in a blob without using php,etc and simply using simple sql insert statement.I am using wamp server for my database.

  1. I know that I should use file system for images instead of using database.But what does a file system actually mean.Does it mean a file or image hosting site whose address will be stored in database.

回答1:


I think that command is a MSSQL syntax. Try this command:

INSERT INTO `abc`
(`img`)
VALUES
(LOAD_FILE('C:/Users/adity/Desktop/New folder/a.png'))

This command stores image as a BLOB




回答2:


Through Mysql workbench, its very easy to load images into database using the following steps.

  1. Right click on the value of the (blob)column in the table and select "Load value from file".
  2. Then we can provide the image path in the system.
  3. Then it will converted into byte array and stored it automatically.
  4. finally save the changes of the table.



回答3:


Below works for me,

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 `abc`
(`img`)
VALUES
(LOAD_FILE('C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/an.jpg')) WHERE id=1;

Hope this helps.



来源:https://stackoverflow.com/questions/34328601/how-to-insert-images-in-blob-in-mysql-table-using-only-sql-syntax-without-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!