Checking if an image field has changed in SQL Server - sql-server-2008

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)

Related

Image is not Inserting in MySQL. Error 1048

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.

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"));

mysql queries before insert operation by syslog-ng

I am using syslog-ng to parse some logs that I am receiving via a csv-parser. However, I want to achieve insert operations that are a bit more complex than the conventional insert using the "destination" option in syslog-ng. Currently, my destination into MYSQL from my syslog-ng conf file looks like this:
destination d_sql_test
{
sql(
type(mysql)
host('<host>')
username('<user>')
password('<pass>')
database('<db_name>')
table('test')
columns('col1')
values('${val1}')
);
};
However, this simply just inserts the contents of val1 into the column col1. I want to be able to specify my insert "logic" as shown in the example in this question.
I am unsure as to where to actually do this, and if it is even supported by syslog-ng
I think you can do this if you can somehow make the decision within syslog-ng.
You could try to use an in-list() filter to check if the username is already listed in a file. If it is not then, you can send the log into the mysql destination, and also to another destination (possibly a program() destination) that updates the file containing the list of users, and reloads the syslog-ng to update the inlist filter.
You can write a syslog-ng template-function in Python that implements the logic somehow, and for example sets a macro to 1 in the message if it should be sent to the database. Then you can use a filter for this macro in your log path with the mysql destination.
Or if you can write a separate destination that does the work in Python: Writing syslog-ng destinations in Python
Also, you might want to post this question on the syslog-ng mailing list, where the developers notice it more easily.

How to add a image to SQL Database from Java

I am making a software and I want to give an option to add a picture.
I am using Netbeans 7.4 , mySQL server 5.1, And Query Browser 1.2
I want to know how to configure the database and how to develop the feature for adding a picture.
You are able to store images in a mySql db using data types such as LONGBLOB
img LONGBLOB not null
To do this in Java you want to do something like the following to get the byte string of the image:
File image = new File(imageName);
BufferedImage bufferedImage = ImageIO.read(image);
WritableRaster writableRaster = bufferedImage.getRaster();
DataBufferByte dataBufferByte = (DataBufferByte) raster.getDataBuffer();
You can then add it to your database using JDBC.
NOTE: Although I have given you the answer on how to do this, I would not recommend implementing it in this way as it is bad practice. Images can get very large and you might find your db filling up very quickly.
Instead, a more efficient solution would be to store the images in directories alongside your code, and store paths to the images in the database as simple text strings.