Home » SQL & PL/SQL » SQL & PL/SQL » How to insert BLOB files into tables (Oracle 11g, Release 11.2.0.2.0, Red Hat)
How to insert BLOB files into tables [message #534249] |
Sun, 04 December 2011 20:14  |
 |
sandy_bridge_655k
Messages: 55 Registered: November 2011
|
Member |
|
|
Hi, I created a music database, and I'm having trouble inserting the audio, video, and lyrics (.doc) into their respective tables. I searched through the forums and found some example code, but I'm not sure how to modify it to fit my purposes.
What I need is a procedure that can insert a complete record into the track table (including an .mp3 file for each row), one that can insert a record into the lyrics table (including .doc file for each row), and a procedure that can insert a single record into the Video table (including an .mv4 file).
Here's the DDL:
CREATE TABLE Artist(
artist_id number(9),
artist_name varchar(30),
country char(2),
num_albums number(3),
num_songs number(5)
);
ALTER TABLE Artist
add CONSTRAINT artist_pk PRIMARY KEY (artist_id);
CREATE TABLE Album(
album_id number(9),
album_title varchar(45),
artist_id number(9),
genre varchar(20),
num_songs_in_album number(3)
);
ALTER TABLE Album
add CONSTRAINT album_pk PRIMARY KEY (album_id);
ALTER TABLE Album
add CONSTRAINT artist_fk FOREIGN KEY (artist_id) REFERENCES Artist(artist_id);
CREATE TABLE Track(
track_id number(9),
artist_id number(9),
album_id number(9),
content BLOB
);
ALTER TABLE Track
add CONSTRAINT track_pk PRIMARY KEY (track_id);
ALTER TABLE Track
add CONSTRAINT album_fk FOREIGN KEY (album_id) REFERENCES Album(album_id);
ALTER TABLE Track
add CONSTRAINT artist2_fk FOREIGN KEY (artist_id) REFERENCES Artist(artist_id);
CREATE TABLE Lyrics (
lyric_id number(9),
track_id number(9),
version varchar(30),
content BLOB
);
ALTER TABLE Lyrics
add CONSTRAINT lyrics_pk PRIMARY KEY ( lyric_id);
ALTER TABLE Lyrics
add CONSTRAINT track_fk FOREIGN KEY (track_id) REFERENCES Track (track_id);
CREATE TABLE Video(
Video_id number(9),
track_id number(9),
producer varchar(25),
director varchar(25),
special_guest varchar(25),
content BLOB,
type char(15)
);
ALTER TABLE Video
add CONSTRAINT video_id PRIMARY KEY (video_id);
ALTER TABLE Video
add CONSTRAiNT track_fk_2 FOREIGN KEY (track_id) REFERENCES Track (track_id);
insert into artist (artist_id, artist_name, country, num_albums, num_songs)
values (1234,'2-PAC','US', 0, 0);
insert into artist (artist_id, artist_name, country, num_albums, num_songs)
values (1235, 'Blink 182' ,'US', 0, 0);
insert into artist (artist_id, artist_name, country, num_albums, num_songs)
values (1236,'James Durbin','US', 0, 0);
insert into artist (artist_id, artist_name, country, num_albums, num_songs)
values (1237, 'Jay-Z' ,'US', 0, 0);
insert into artist (artist_id, artist_name, country, num_albums, num_songs)
values (1238,'Katy Perry','US', 0, 0);
insert into artist (artist_id, artist_name, country, num_albums, num_songs)
values (1239, 'Machine Head' ,'US', 0, 0);
insert into artist (artist_id, artist_name, country, num_albums, num_songs)
values (1240,'Megadeth','US', 0, 0);
insert into artist (artist_id, artist_name, country, num_albums, num_songs)
values(1241, 'Nickelback', 'US', 0, 0);
insert into artist (artist_id, artist_name, country, num_albums, num_songs)
values (1242,'Nirvana','US', 0, 0);
insert into artist (artist_id, artist_name, country, num_albums, num_songs)
values (1243, 'Outkast' ,'US', 0, 0);
insert into artist (artist_id, artist_name, country, num_albums, num_songs)
values (1244,'Pearl Jam','US', 0, 0);
insert into artist (artist_id, artist_name, country, num_albums, num_songs)
values (1245, 'Stick to Your Guns' ,'US', 0, 0);
insert into artist (artist_id, artist_name, country, num_albums, num_songs)
values (1246,'Switchfoot','US', 0, 0);
insert into artist (artist_id, artist_name, country, num_albums, num_songs)
values(1247, 'The Pharcyde', 'US', 0, 0);
insert into album (album_id, album_title, artist_id, genre, num_songs_in_album)
values (4321, 'Me Against The World', 1234, 'Hip Hop', 0);
insert into album (album_id, album_title, artist_id, genre, num_songs_in_album)
values (4322, 'Enema of the State', 1235, 'Alternative Rock', 0);
insert into album (album_id, album_title, artist_id, genre, num_songs_in_album)
values (4323, 'Memories of a Beautiful Disaster', 1236, 'Alternative Rock', 0);
insert into album (album_id, album_title, artist_id, genre, num_songs_in_album)
values (4324, 'The Blueprint', 1237, 'Hip Hop', 0);
insert into album (album_id, album_title, artist_id, genre, num_songs_in_album)
values (4325, 'Teenage Dream', 1238, 'Pop', 0);
insert into album (album_id, album_title, artist_id, genre, num_songs_in_album)
values (4326, 'Unto the Locust', 1239, 'Alternative Rock', 0);
insert into album (album_id, album_title, artist_id, genre, num_songs_in_album)
values (4327, 'Th1rt3en', 1240, 'Metal', 0);
insert into album (album_id, album_title, artist_id, genre, num_songs_in_album)
values (4328, 'Here and Now', 1241, 'Pop', 0);
insert into album (album_id, album_title, artist_id, genre, num_songs_in_album)
values (4329, 'Nevermind', 1242, 'Alternative Rock', 0);
insert into album (album_id, album_title, artist_id, genre, num_songs_in_album)
values (4330, 'Southernplayalisticadillacmuzik', 1243, 'Hip Hop', 0);
insert into album (album_id, album_title, artist_id, genre, num_songs_in_album)
values (4331, 'Rearview Mirrors', 1244, 'Alternative Rock', 0);
insert into album (album_id, album_title, artist_id, genre, num_songs_in_album)
values (4332, 'For What Its Worth', 1245, 'Alternative Rock', 0);
insert into album (album_id, album_title, artist_id, genre, num_songs_in_album)
values (4333, 'Vice Verses', 1246, 'Rock', 0);
insert into album (album_id, album_title, artist_id, genre, num_songs_in_album)
values (4334, 'Bizarre Ride II', 1247, 'Hip Hop', 0);
[Updated on: Sun, 04 December 2011 23:22] by Moderator Report message to a moderator
|
|
|
|
Re: How to insert BLOB files into tables [message #534251 is a reply to message #534250] |
Sun, 04 December 2011 20:59   |
 |
sandy_bridge_655k
Messages: 55 Registered: November 2011
|
Member |
|
|
I just wrote this procedure after reading a bit of literature, but I want to make sure it will execute correctly before I run it.
How do I make this procedure look inside the right directory on my client machine (i.e. the directory where the .mp3 files are stored)? I'm connected to an Amazon RDS DB instance on Amazon's AWS cloud.
CREATE OR REPLACE PROCEDURE insert_track (
track_idno number,
artist_idno number,
album_idno number,
pfname varchar2) IS
src_file BFILE;
dst_file BLOB;
lgh_file BINARY_INTEGER;
BEGIN
src_file := bfilename('TEMP', pfname);
-- insert a NULL record to lock
INSERT INTO track
(track_id, artist_id, album_id, content)
VALUES
(track_idno, artist_idno, album_idno, EMPTY_BLOB())
RETURNING content INTO dst_file;
-- lock record
SELECT content
INTO dst_file
FROM track
WHERE artist_id = artist_idno
AND album_id = album_idno
AND track_id = track_idno
FOR UPDATE;
-- open the file
dbms_lob.fileopen(src_file, dbms_lob.file_readonly);
-- determine length
lgh_file := dbms_lob.getlength(src_file);
-- read the file
dbms_lob.loadfromfile(dst_file, src_file, lgh_file);
-- update the blob field
UPDATE track
SET content = dst_file
WHERE track_id = track_idno
AND artist_id = artist_idno
AND album_id = album_idno;
-- close file
dbms_lob.fileclose(src_file);
END insert_track;
Will this work?
create or replace directory temp as '/home';
grant read on directory temp to username;
[Updated on: Sun, 04 December 2011 21:00] Report message to a moderator
|
|
|
|
|
|
|
|
|
|
|
Re: How to insert BLOB files into tables [message #534260 is a reply to message #534258] |
Sun, 04 December 2011 23:22   |
 |
sandy_bridge_655k
Messages: 55 Registered: November 2011
|
Member |
|
|
OK, I transferred all the above code to my Oracle 10g XE on my desktop computer.
After building the database, I entered the follow commands at the SQL prompt:
create or replace directory temp as
'/home/big-geek/Dropbox/Graduate work/Applied Database Technologies/Music';
Directory created.
When I ran the procedure insert_track, I received the following error:
exec insert_track(1, 1234, 4321, 'Dear-Mama.m4a');
BEGIN insert_track(1, 1234, 4321, 'Dear-Mama.m4a'); END;
*
ERROR at line 1:
ORA-22288: file or LOB operation FILEOPEN failed
Permission denied
ORA-06512: at "SYS.DBMS_LOB", line 523
ORA-06512: at "STEPHEN.INSERT_TRACK", line 30
ORA-06512: at line 1
It seems that I don't have permission to open the file. How do I grant myself this permission?
[Updated on: Mon, 05 December 2011 01:06] by Moderator Report message to a moderator
|
|
|
|
Re: How to insert BLOB files into tables [message #534262 is a reply to message #534261] |
Sun, 04 December 2011 23:49   |
 |
sandy_bridge_655k
Messages: 55 Registered: November 2011
|
Member |
|
|
yeah, I just realized that I specified the path wrong after looking through the directory at the terminal.
I tried the following, but received the same error:
SQL> create or replace directory temp as
'/home/big-geek/Dropbox/Graduate\ work/Applied\ Database\ Technologies/Music';
Directory created.
SQL> exec insert_track(1, 1234, 4321, 'Dear-Mama.m4a');
BEGIN insert_track(1, 1234, 4321, 'Dear-Mama.m4a'); END;
*
ERROR at line 1:
ORA-22288: file or LOB operation FILEOPEN failed
Permission denied
ORA-06512: at "SYS.DBMS_LOB", line 523
ORA-06512: at "STEPHEN.INSERT_TRACK", line 30
ORA-06512: at line 1
SQL> create or replace directory temp as
'/home/big-geek/Dropbox/Graduate\work/Applied\Database\Technologies/Music';
Directory created.
SQL> exec insert_track(1, 1234, 4321, 'Dear-Mama.m4a');
BEGIN insert_track(1, 1234, 4321, 'Dear-Mama.m4a'); END;
*
ERROR at line 1:
ORA-22288: file or LOB operation FILEOPEN failed
Permission denied
ORA-06512: at "SYS.DBMS_LOB", line 523
ORA-06512: at "STEPHEN.INSERT_TRACK", line 30
ORA-06512: at line 1
solution?
[Updated on: Mon, 05 December 2011 02:43] by Moderator Report message to a moderator
|
|
|
|
Re: How to insert BLOB files into tables [message #534266 is a reply to message #534263] |
Mon, 05 December 2011 00:06   |
 |
sandy_bridge_655k
Messages: 55 Registered: November 2011
|
Member |
|
|
Thanks for the link.
I followed the steps from the existing post, but still received the same error. Is the insert_track procedure code wrong?
Enter user-name: system
Enter password:
Connected to:
Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production
SQL> create or replace directory temp as
'home/big-geek/Dropbox/Graduate\ work/Applied\ Database\ Technologies/Music';
Directory created.
SQL> grant read on directory temp to stephen;
Grant succeeded.
SQL> connect stephen/password
Connected.
SQL> exec insert_track(1, 1234, 4321, 'Dear-Mama.m4a');
BEGIN insert_track(1, 1234, 4321, 'Dear-Mama.m4a'); END;
*
ERROR at line 1:
ORA-22288: file or LOB operation FILEOPEN failed
No such file or directory
ORA-06512: at "SYS.DBMS_LOB", line 523
ORA-06512: at "STEPHEN.INSERT_TRACK", line 30
ORA-06512: at line 1
[Updated on: Mon, 05 December 2011 02:43] by Moderator Report message to a moderator
|
|
|
|
|
|
|
|
|
Re: How to insert BLOB files into tables [message #534282 is a reply to message #534281] |
Mon, 05 December 2011 01:26   |
 |
sandy_bridge_655k
Messages: 55 Registered: November 2011
|
Member |
|
|
All the spaces are removed at this point, and I'm still receiving the same error. I renamed all the sub directories at the OS level such that are no spaces in the path.
big-geek@big-geek-Ubuntu-11:~/Dropbox/Graduate work/Applied Database Technologies/Music$ dir
01\ When\ We\ Stand\ Together.m4a All\ the\ Small\ Things.m4v
02\ Public\ Enemy\ No.\ 1.m4a Dear-Mama.m4a
03\ Izzo\ (H.O.V.A.).m4a Dear\ Mama.m4v
04\ Restless.m4a Even\ Flow.m4v
05\ Love\ Me\ Bad.m4a Izzo\ (H.O.V.A.).m4v
05\ Smells\ Like\ Teen\ Spirit.m4a Locust.m4v
06\ All\ the\ Small\ Things.m4a Love\ Me\ Bad.m4v
07\ Player's\ Ball.m4a Player's\ Ball.m4v
07\ This\ Is\ More.m4a Public\ Enemy\ No.\ 1.m4v
1-03\ Even\ Flow.m4a Restless.m4v
1-07\ The\ One\ That\ Got\ Away.m4a Smells\ Like\ Teen\ Spirit.m4v
12\ Passin'\ Me\ By.m4a The\ One\ That\ Got\ Away.m4v
12\ When\ We\ Stand\ Together.m4v This\ is\ More.m4v
create or replace directory temp as
'home/big-geek/Dropbox/GraduateWork/AppliedDatabaseTechnologies/Music';
Directory created.
SQL> grant read on directory temp to stephen;
Grant succeeded.
SQL> connect stephen/4uq5LiwN2cjvy3i
Connected.
SQL> exec insert_track(1, 1234, 4321, 'Dear-Mama.m4a');
BEGIN insert_track(1, 1234, 4321, 'Dear-Mama.m4a'); END;
*
ERROR at line 1:
ORA-22288: file or LOB operation FILEOPEN failed
No such file or directory
ORA-06512: at "SYS.DBMS_LOB", line 523
ORA-06512: at "STEPHEN.INSERT_TRACK", line 30
ORA-06512: at line 1
[Updated on: Mon, 05 December 2011 02:43] by Moderator Report message to a moderator
|
|
|
Re: How to insert BLOB files into tables [message #534283 is a reply to message #534282] |
Mon, 05 December 2011 01:36   |
flyboy
Messages: 1903 Registered: November 2006
|
Senior Member |
|
|
Just one remark: you created that directory in Oracle as relative path (probably to some Oracle one). According to its content (starting with home directory), is it not rather absolute one? Then, it should start with '/' in the Oracle definition.
|
|
|
|
Re: How to insert BLOB files into tables [message #534286 is a reply to message #534283] |
Mon, 05 December 2011 01:48   |
 |
sandy_bridge_655k
Messages: 55 Registered: November 2011
|
Member |
|
|
Appending '/' to the front of the path did not solve the problem:
SQL> create or replace directory temp as
'/home/big-geek/Dropbox/GraduateWork/AppliedDatabaseTechnologies/Music';
Directory created.
SQL> grant read on directory temp to stephen;
Grant succeeded.
SQL> connect stephen/password
Connected.
SQL> exec insert_track(1, 1234, 4321, 'Dear-Mama.m4a');
BEGIN insert_track(1, 1234, 4321, 'Dear-Mama.m4a'); END;
*
ERROR at line 1:
ORA-22288: file or LOB operation FILEOPEN failed
Permission denied
ORA-06512: at "SYS.DBMS_LOB", line 523
ORA-06512: at "STEPHEN.INSERT_TRACK", line 30
ORA-06512: at line 1
[Updated on: Mon, 05 December 2011 02:42] by Moderator Report message to a moderator
|
|
|
|
Re: How to insert BLOB files into tables [message #534289 is a reply to message #534285] |
Mon, 05 December 2011 01:50   |
 |
sandy_bridge_655k
Messages: 55 Registered: November 2011
|
Member |
|
|
Line 30 is when the fileopen occurs.
insert_track (
2 track_idno number,
3 artist_idno number,
4 album_idno number,
5 pfname varchar2) IS
6
7 src_file BFILE;
8 dst_file BLOB;
9 lgh_file BINARY_INTEGER;
10 BEGIN
11 src_file := bfilename('TEMP', pfname);
12
13 -- insert a NULL record to lock
14 INSERT INTO track
15 (track_id, artist_id, album_id, content)
16 VALUES
17 (track_idno, artist_idno, album_idno, EMPTY_BLOB())
18 RETURNING content INTO dst_file;
19
20 -- lock record
21 SELECT content
22 INTO dst_file
23 FROM track
24 WHERE artist_id = artist_idno
25 AND album_id = album_idno
26 AND track_id = track_idno
27 FOR UPDATE;
28
29 -- open the file
30 dbms_lob.fileopen(src_file, dbms_lob.file_readonly);
31
32 -- determine length
33 lgh_file := dbms_lob.getlength(src_file);
34
35 -- read the file
36 dbms_lob.loadfromfile(dst_file, src_file, lgh_file);
37
38 -- update the blob field
39 UPDATE track
40 SET content = dst_file
41 WHERE track_id = track_idno
42 AND artist_id = artist_idno
43 AND album_id = album_idno;
44
45 -- close file
46 dbms_lob.fileclose(src_file);
47 END insert_track;
48 /
SQL> exit
|
|
|
Re: How to insert BLOB files into tables [message #534291 is a reply to message #534286] |
Mon, 05 December 2011 01:54   |
flyboy
Messages: 1903 Registered: November 2006
|
Senior Member |
|
|
sandy_bridge_655k wrote on Mon, 05 December 2011 08:48Appending '/' to the front of the path did not solve the problem:
I would disagree with you. Or, are you blind not to see that the error message changed? Now it is "Permission denied", which means, Oracle (OS user oracle) cannot access that directory. What is its permission for Others group? Maybe you should change it to at least "r".
|
|
|
|
Re: How to insert BLOB files into tables [message #534294 is a reply to message #534293] |
Mon, 05 December 2011 02:23   |
flyboy
Messages: 1903 Registered: November 2006
|
Senior Member |
|
|
sandy_bridge_655k wrote on Mon, 05 December 2011 09:19How do I change the permission for Others group to "r"?
Using chmod command (for both directory and file/s).
Maybe you should take some Linux basics lessons. Or, better, consult it with a "big geek" (yes, your OS name sounds funny in comparison with your Linux knowledge).
|
|
|
|
|
|
|
Re: How to insert BLOB files into tables [message #534389 is a reply to message #534307] |
Mon, 05 December 2011 16:07   |
 |
sandy_bridge_655k
Messages: 55 Registered: November 2011
|
Member |
|
|
I need to create a procedure which finds all the videos of the artist, given the artist name.
I added a video_name column to the video table to make this easier (i.e avoided reading the file name of the BLOB).
I wrote the procedure below, but I think it's missing a loop. I would appreciate if anyone could help revise this procedure with any necessary corrections....
CREATE OR REPLACE PROCEDURE find_artist_videos (
a_name varchar2) IS
aid number;
tid number;
BEGIN
SELECT artist_id INTO aid FROM artist
WHERE artist_name = a_name;
SELECT track_id INTO tid FROM track --there could be multiple tracks from the same artist
WHERE artist_id = aid; --Do I need a loop around all these select statements?
SELECT video_name FROM video
WHERE track_id = tid;
END find_artist_video;
[Updated on: Mon, 05 December 2011 16:35] Report message to a moderator
|
|
|
|
|
|
|
Goto Forum:
Current Time: Mon Jun 02 17:26:08 CDT 2025
|