I want to store an image in a MySQL database. I have created a table with a BLOB datatype, but now how do I store the image in this table?
+4
A:
read the content of the file (BINARY) and insert it in insert \ update MYSQL query
a lot of example in google :
http://www.java2s.com/Code/Java/Database-SQL-JDBC/InsertpicturetoMySQL.htm
Haim Evgi
2009-12-21 09:46:02
+7
A:
You may want to check out the following example:
From java2s.com: Insert picture to MySQL:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class InsertPictureToMySql {
public static void main(String[] args) throws Exception, IOException, SQLException {
Class.forName("org.gjt.mm.mysql.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/databaseName", "root", "root");
String INSERT_PICTURE = "INSERT INTO MyPictures (photo) VALUES (?)";
FileInputStream fis = null;
PreparedStatement ps = null;
try {
conn.setAutoCommit(false);
File file = new File("/tmp/photo.png");
fis = new FileInputStream(file);
ps = conn.prepareStatement(INSERT_PICTURE);
ps.setBinaryStream(1, fis, (int) file.length());
ps.executeUpdate();
conn.commit();
} finally {
ps.close();
fis.close();
}
}
}
MySQL Table:
CREATE TABLE MyPictures (
photo BLOB
);
If the image is located on your MySQL server host, you could use the LOAD_FILE()
command from a MySQL client:
INSERT INTO MyPictures (photo) VALUES(LOAD_FILE('/tmp/photo.png'));
Daniel Vassallo
2009-12-21 09:46:36
Can we insert the image using simple query language?
Karthik.m
2009-12-21 09:51:00
Yes, you may want to try using the `LOAD_FILE()` function: `INSERT INTO mytbl (image_data) VALUES(LOAD_FILE('/tmp/myimage.png'));` (http://www.freeopenbook.com/mysqlcookbook/mysqlckbk-CHP-17-SECT-7.html)
Daniel Vassallo
2009-12-21 09:56:13
Its not working mate....
Karthik.m
2009-12-21 10:14:26
Karthik, what is not working?... The `LOAD_FILE()` method? If yes, make sure that the file is readable by mysql, and also make sure that your mysql user has the `FILE` privilege.
Daniel Vassallo
2009-12-21 10:17:00
To grant the `FILE` privilege, log-in as root and execute `GRANT FILE ON *.* TO 'mysql_user'@'localhost'`.
Daniel Vassallo
2009-12-21 10:25:41
@Daniel : how to check whether my MySQl has File privilege, if i am not having it how to give the privilege and also how to store the image file in server host?
Hari
2009-12-21 10:26:48
@Hari: To check the privileges of the current user, log-in with your user in mysql `mysql -h localhost -u user -p` and execute `SHOW GRANTS;`. To grant the `FILE` privilege, please check my previous comment.
Daniel Vassallo
2009-12-21 10:33:22
@Daniel : how to put the image file into the server host?
Hari
2009-12-21 10:35:38
@Hari: To put the image on the server host, you will have to upload it yourself (or through a script) on your server's file system. In fact, this is not the ideal method to insert images from a web page, since the uploading to the file system is useless.
Daniel Vassallo
2009-12-21 10:42:15
@Daniel : so is it better to add a image into server rather than a adding it into the database right......
Hari
2009-12-21 10:47:22
@Karthik: There is an issue on the MySQL bug-tracker http://bugs.mysql.com/bug.php?id=38403, which is reporting the same behaviour that you are experiencing. Apparently one user solved it by stopping apparmor (which is a security servicelike selinux).
Daniel Vassallo
2009-12-21 10:50:28
@Hari: You may want to check out this Stack Overflow post: http://stackoverflow.com/questions/3748/
Daniel Vassallo
2009-12-21 10:51:37
@Daniel :ya thanks .. i saw the same post before coming to this one... i thought this post is also having some similarities to that one and also it that one no one told how to insert a image through a query as u have mentioned...
Hari
2009-12-21 10:56:05
@Down voter: Canu post the reason for Downvoting!!!!!!!!!!!
Hari
2009-12-21 10:57:15
@Hari: It doesn't look like the question or any of the answers were down-voted :)
Daniel Vassallo
2009-12-21 11:10:29
@Daniel :actually the number of up votes was 5 before...and now its 4..just wanted to know that whether any other posible answer is also there...
Hari
2009-12-21 11:18:07
and now its 3...
Hari
2009-12-21 11:21:35
@Hari: Someone undid the upvote then. (Not a down-vote)
Daniel Vassallo
2009-12-21 11:21:41
okie.... thanks for the information provided...it very helpful to me...
Hari
2009-12-21 11:25:27