Image is not Inserting in MySQL. Error 1048 - mysql

I am facing error(in arrow (2)) while executing query( in arrow(1)) and also unable to view image. Name path and spelling of image location is correct.
Screen shot of query output

The error is telling you that the result of
load_file('C:/hr.jpg')
results in null. You will probably need to fix the path to
load_file('C:\\hr.jpg')

Step 1: Execute following query. This will show path where you have to upload image.
select ##GLOBAL.secure_file_priv;
Result: C:\ProgramData\MySQL\MySQL Server 8.0\Uploads\ [In my case]
Step 2: Upload image in Uploads folder.
Step 3: Execute query
create table display (SR_no int,picture longblob not null ) ;
We can user either blob or long blob for image.
insert into display value (1,load_file('C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/shiv1.jpg'));
Step 4: Image will insert successfully.

LOAD_DATA only works for files located in the same host as the MySQL server. In your case, I guess the MySQL server is remote.
To load data from a local file to a remote server, you need to use LOAD DATA LOCAL instead.

Related

MySQL secure-file-priv=""

I have next problem: I want to insert in a table an image (LONGBLOB). First of all, I successfully inserted the image from C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Uploads (secure-file-priv = "C:\ProgramData\MySQL\MySQL Server 8.0\Uploads") and then I set secure-file-priv="" in order to insert images from any location but NULL value is inserted. Can I upload images from any location? If so, how?
This is my table:
And this is the query:
INSERT INTO `soccertrackerdev`.`test`(`id`,`image`)
VALUES(4, LOAD_FILE('C:/Users/ionut.puiu/Desktop/Untitled.png'));
And the message after run the query is:
When you set this in your ini file, MySQL starts to obey:
secure-file-priv=""
(The setting for local_infile does not matter, in this case.)
This must be the difference between empty and set to NULL in the docs:
If empty, the variable has no effect. This is not a secure setting.
If set to the name of a directory, the server limits import and export operations to work only with files in that directory. The directory must exist; the server does not create it.
If set to NULL, the server disables import and export operations.

Updating BLOB from MySQL Workbench

I have a problem with one SQL statement. I am trying to update something in a table which is a Blob field with a pdf. And it's not working.
UPDATE employees
SET resume = LOAD_FILE('C:\Users\gaby\Desktop\sample_resume.pdf')
WHERE id = 1;
If I use the above command the result is null. If I use the below command the result is not null, there is a pdf but it is empty.
UPDATE employees
SET resume = 'C:\Users\gaby\Desktop\sample_resume.pdf'
WHERE id = 1;
I can do this update using JDBC, it works perfect. But I want to do it from workbench too.
Thanks in advance!
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,
update employees set file=LOAD_FILE('C:/ProgramData/MySQL/MySQL Server 5.7/Uploads/fileName.jpg') where id = 1;
Hope this helps.
Check if all conditions described in the manual are given.
LOAD_FILE(file_name)
Reads the file and returns the file contents as a string. To use this function, the file must be located on the server host, you must specify the full path name to the file, and you must have the FILE privilege. The file must be readable by all and its size less than max_allowed_packet bytes. If the secure_file_priv system variable is set to a nonempty directory name, the file to be loaded must be located in that directory.
If the file does not exist or cannot be read because one of the preceding conditions is not satisfied, the function returns NULL.
load_file() returns NULL, (at least) one of the conditions must fail.

When I insert a image into MySQL table the result is NULL

What's wrong with this code, I want to insert an image to table, but when I was executed this code the result of image field is NULL.
I try with MySQL Workbench executing:
CREATE TABLE image(keyh int, img blob);
INSERT INTO image VALUES(1, load_file('d:\Picture\cppLogo.png'));
To use this function, the file must be located on the server host, you
must specify the full path name to the file, and you must have the
FILE privilege. The file must be readable by all and its size less
than max_allowed_packet bytes. If the secure_file_priv system variable
is set to a nonempty directory name, the file to be loaded must be
located in that directory.
If the file does not exist or cannot be read because one of the
preceding conditions is not satisfied, the function returns NULL.
http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_load-file
What can you do?
Check which user mysql is running with, and make sure the file is readable by that user. Make sure the security settings allow the file to be read and it is not of greater size than max_allowed_packet.
See SHOW VARIABLES LIKE 'max_allowed_packet'.
For me, it looks like the file is on your localhost and you try to upload it. This is not possible using LOAD_FILE(). The file must be already on the server.
The issue can also be caused by your windows directory seperator character \ (like RiggsFolly said), which is used for escaping instead, switch to unix style / then:
LOAD_FILE('D:/Picture/cppLogo.png')
Or your Image is of greater filesize than a BLOB field can hold, like Balazs Vago said.
i was found the correct syntax is following this:
C:/wamp/binsql5.5.20/data/56VRLRFE.jpg'
not this
C:\wamp\binsql5.5.20\data\56VRLRFE.jpg'
thanks guys for all your Answer :D
Open your MySql Command Line Client and login with root user and type
mysql> SHOW VARIABLES LIKE "secure_file_priv";
this will show you the secure path used by MySql to access the files. something like
+------------------+-----------------------+
| Variable_name | Value |
+------------------+-----------------------+
| secure_file_priv | /var/lib/mysql-files/ |
+------------------+-----------------------+
you can either paste files inside this folder or change the "secure_file_priv" variable value to "empty string" so that it can read file from anywhere.
On Windows the fundamental problem is that MySql, by default, runs as a Windows service under the Network account which means that there are only a few file locations the server can access. Thus for load_file to work, the file must be placed in a folder on the server which can be read by the service. There seems to be no documentation on this. In my investigation the only folder that works with load_file is C:\ProgramData\MySQL\MySQL Server 8.0\Uploads
Run a query to test the load...
select load_file('C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Uploads\\1.txt') ;
Note on windows you have to use either double \ or / to separate the path elements. This will return NULL on failure, otherwise the contents of the file.
Assume now a table named db.image with columns source and image. Source is character and image is blob. The command to load a.jpg into the table would be
insert into db.image (source,image) values ('a.jpg',load_file('c:/programdata/mysql/mysql server 8.0/uploads/a.jpg'));
Store Directly Without folder name for example-
create table myimg(id int, image mediumblob);
insert into myimg values(101, load_file("E://xyz.png"));

Checking if an image field has changed in SQL Server

We have a database where the images are stored as image column types in a table. Periodically, we want to fire off a process that goes and checks to see if the images have been updated.
The process would:
Download the file from the source over HTTP
Check if the file is different from the db version
Update the db if necessary
I am not sure how to go about the second step. How would I check to see if the files are different?
EDIT:
How accurate would checking the image's size using DATALENGTH ?
Download the file as byte[] array
Retrieve the DATALENGTH of the BLOB field from the SQL server:
SELECT DATALENGTH(Content) AS ItemLength ...
Compare the two. If they are equal, then they are the same image:
byte[].Length == BlobLength (long)

Insert LargeBlog sql issue

Right I seem to be having a issue inserting a image into a database (don't ask why lol)
If tryed a simple
UPDATE player SET Image = load_file('94.jpg') WHERE id =94;
And had the image directly located from where im running the sql commands from
UPDATE player
SET Image = load_file('C:\Users\***\Documents\databases\Scripts\94.jpg')
WHERE id =94;
I have also tried the above with the full path but when i query the table it shows the Image column of type LargeBlog as empty!
Any ideas????
The file must be on the machine MySQL is installed on.
I have managed to fix it with the use of forward slashes instead of back slashes. So im getting it into the database now... all goood
but i want to do relative paths and as i said ('94.jpg') doesn't work. When its placed my C:\Wamp directory... Which is the same place I put sql scripts when I run them with a simple
source script.sql; <-Notice no path required and it works. So I assumed that MySql default directory was the Wamp Folder