How Can i Store photo (image) in column of table
2006-08-23 19:17
375 查看
This assumes that the images in question currently reside on the database server. If you want to upload images into the database from a client machine, you will need an extra step where you move the files to the appropriate directory or directories on the server. This example will store the images in a BLOB column, rather than a much more flexible interMedia ORDImage column. A BLOB column is fine if you only want to store and retrieve the image, but interMedia gives you significantly more flexibility to manipulate the image, extract metadata, etc.
Create the directory
First, you will want to tell Oracle where it can find the images you want to insert into the database. The easiest way to do this is to create a directory object. In this case, I will be loading images from 'c:/temp/img/'.
CREATE DIRECTORY images AS 'c:/temp/img/'
If you don't have the CREATE DIRECTORY privilege, you will need to have someone else, like your DBA, create the directory and grant you READ access to it.
GRANT READ ON DIRECTORY images TO scott;
Create the Table to Hold the Images
CREATE TABLE images (
image_name VARCHAR2( 100 ),
mime_type VARCHAR2( 100 ),
content BLOB
)
Create a Stored Procedure to Load the Image
CREATE OR REPLACE PROCEDURE load_image( file_name VARCHAR2 )
AS
file_lob BFILE;
binary_lob BLOB;
mime_type images.mime_type%type;
extension_pos NUMBER;
BEGIN
-- Use the extension of the file name to determing the MIME-type
-- The MIME-type for a .JPG file will be image/jpg
extension_pos := INSTR( file_name, '.' );
mime_type := 'image/' || SUBSTR( file_name,
extension_pos + 1,
LENGTH( file_name ) );
-- Insert a new row into the images table. Get the LOB locator
-- for the newly inserted row-- we will be using that to insert
-- the content from the file.
INSERT INTO IMAGES( image_name, mime_type, content )
VALUES( file_name, mime_type, empty_blob() )
RETURNING content INTO binary_lob;
-- Open up the file in the IMAGES directory named file_name,
-- load that file into the newly created LOB locator, and
-- close the file
file_lob := BFILENAME( 'IMAGES', file_name );
dbms_lob.fileOpen ( file_lob, dbms_lob.file_readOnly );
dbms_lob.loadFromFile( binary_lob,
file_lob,
dbms_lob.getLength( file_lob ) );
dbms_lob.fileClose ( file_lob );
END;
Load the Image
scott@jcave > exec load_image( 'sample.jpg' );
PL/SQL procedure successfully completed.
scott@jcave > column image_name format a30;
scott@jcave > column mime_type format a15;
scott@jcave > column length format 999999;
scott@jcave >
1 SELECT image_name, mime_type,dbms_lob.getLength( content ) length
2 FROM images;
no rows selected
Elapsed: 00:00:00.00
scott@jcave > /
IMAGE_NAME MIME_TYPE LENGTH
------------------------------ --------------- -------
sample.jpg image/jpg 9894
Regards
Lakmal
Create the directory
First, you will want to tell Oracle where it can find the images you want to insert into the database. The easiest way to do this is to create a directory object. In this case, I will be loading images from 'c:/temp/img/'.
CREATE DIRECTORY images AS 'c:/temp/img/'
If you don't have the CREATE DIRECTORY privilege, you will need to have someone else, like your DBA, create the directory and grant you READ access to it.
GRANT READ ON DIRECTORY images TO scott;
Create the Table to Hold the Images
CREATE TABLE images (
image_name VARCHAR2( 100 ),
mime_type VARCHAR2( 100 ),
content BLOB
)
Create a Stored Procedure to Load the Image
CREATE OR REPLACE PROCEDURE load_image( file_name VARCHAR2 )
AS
file_lob BFILE;
binary_lob BLOB;
mime_type images.mime_type%type;
extension_pos NUMBER;
BEGIN
-- Use the extension of the file name to determing the MIME-type
-- The MIME-type for a .JPG file will be image/jpg
extension_pos := INSTR( file_name, '.' );
mime_type := 'image/' || SUBSTR( file_name,
extension_pos + 1,
LENGTH( file_name ) );
-- Insert a new row into the images table. Get the LOB locator
-- for the newly inserted row-- we will be using that to insert
-- the content from the file.
INSERT INTO IMAGES( image_name, mime_type, content )
VALUES( file_name, mime_type, empty_blob() )
RETURNING content INTO binary_lob;
-- Open up the file in the IMAGES directory named file_name,
-- load that file into the newly created LOB locator, and
-- close the file
file_lob := BFILENAME( 'IMAGES', file_name );
dbms_lob.fileOpen ( file_lob, dbms_lob.file_readOnly );
dbms_lob.loadFromFile( binary_lob,
file_lob,
dbms_lob.getLength( file_lob ) );
dbms_lob.fileClose ( file_lob );
END;
Load the Image
scott@jcave > exec load_image( 'sample.jpg' );
PL/SQL procedure successfully completed.
scott@jcave > column image_name format a30;
scott@jcave > column mime_type format a15;
scott@jcave > column length format 999999;
scott@jcave >
1 SELECT image_name, mime_type,dbms_lob.getLength( content ) length
2 FROM images;
no rows selected
Elapsed: 00:00:00.00
scott@jcave > /
IMAGE_NAME MIME_TYPE LENGTH
------------------------------ --------------- -------
sample.jpg image/jpg 9894
Regards
Lakmal
相关文章推荐
- 《How hard can it be? Estimating the difficulty of visual search in an image》和 PPT
- How can I put a checkbox in a column of my DataGrid?
- How can I set the background of UITableView (the tableview style is "Grouped") to use an image?
- How can I put a checkbox in a column of my DataGrid?
- Solution for Can’t update table ‘t1′ in stored function/trigger because it is already used by statement which invoked this store
- msyql同步的时候报错 : 错误代码: 1293 Incorrect table definition;there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
- How to Cheat in Photoshop CS3: The art of creating photorealistic montages
- How can I dump only certain entries of a table based on my condition?
- Refreshing One Column based on the value of Another Column in ADFdi Table
- ADF - Groovy for Total Sum of a Column in a Table - adf表格加合计
- In how many ways can we select some of these coins so that they are X yen in total?
- How can we specify src attribute of img tag in CSS?
- How to use outline levels to create a table of contents (TOC) in Word 2003 and in Word 2002
- 58.The PART_CODE column in the SPARES table contains the following list of values:
- How to store an image file to SQliteDB in android
- Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAUL
- I want to do custom handling of special keys such as the Tab key or F2 in the TextBox of a column in the DataGrid. How do I subc
- An explicit value for the identity column in table 'users' can only be specified when a column list
- An explicit value for the identity column in table can only be specified when a column list is used and IDENTITY_INSERT is ON
- How You Can Cash In On The Green Gold Rush of 2014