Inserting BLOB into table
HI, I am a newbie in PL/SQL, but I have a question about LOBs. My code below is to insert an image in a table. My question is, if I decide to return at a later date and perform my procedure once more to add another pict6.jpg at the table, would this procedure remove existing photos of the table and add only pict6.jpg? The reason I ask is because the update command line sets the column photo = empty_blob() every time that the anonymous block is run. If this is the case, what would be a better method to load images into a table? Thanks again for your time. My code is below, please excuse my notes, which I'm still learning:
GRANT CONNECT, RESOURCE to scott;
GRANT CREATE ANY DIRECTORY TO scott;
GRANT DROP ANY DIRECTORY to scott;
CREATE or CHANGE in ADPHOTO_DIR the DIRECTORY "C:\Temp".
/
GRANT READ ON DIRECTORY ADPHOTO_DIR to scott;
CONNECT scott/tiger2;
COMMIT;
-ALTER TABLE StudentRecord ADD (Photograph BLOB); <-can be used instead of the below -.
CREATE OR REPLACE StudentRecord TABLE
(First name VARCHAR2 (20) not null,)
LastName VARCHAR2 (40) not null,
HomeworkAvg NUMBER (6.2),
AttendanceAvg NUMBER (6.2),
ExamAvg NUMBER (6.2),
StudentID VARCHAR2 (10),
Photographing the BLOB
CONSTRAINT check_FirstName_LastName CHECK (FirstName! = LastName As String)
);
CREATE OR REPLACE PROCEDURE load_blob_from_file
(p_photo VARCHAR2)
AS
f_photo BFILE.
b_photo BLOB;
BEGIN
UPDATE StudentRecord SET photo = empty_blob()
RETURN to photograph IN b_photo;
f_photo: = bfilename ('ADPHOTO_DIR', p_photo);
DBMS_LOB. FileOpen (f_photo, dbms_lob.file_readonly);
DBMS_LOB. LoadFromFile (b_photo, f_photo, dbms_lob.getlength (f_photo));
DBMS_LOB. FileClose (f_photo);
commit;
EXCEPTION
WHILE others THEN
dbms_output.put_line ('* ERROR * check your procedure.) ") ;
end;
/
-Run the load_blob_from_file procedure to insert the first picture in the table StudentRecord
EXECUTE load_blob_from_file ('pict1.jpg');
commit;
EXECUTE load_blob_from_file ('pict2.jpg');
commit;
EXECUTE load_blob_from_file ('pict3.jpg');
commit;
EXECUTE load_blob_from_file ('pict4.jpg');
commit;
EXECUTE load_blob_from_file ('pict5.jpg');
commit;
-Test to see if it worked
SELECT FirstName, dbms_lob.getlength (Photograph) "Photo Size"
OF StudentRecord;
Your procedure works only if there is always exactly 1 row in the table of StudentRecord. It seems highly unlikely - you probably wouldn't need store photos if your school takes care of a student.
Your definition of the procedure does not seem particularly reasonable. Of course, the goal is not to load a photo into the table, it must load the photo online of an individual to the student in the table. This means that your procedure should take a parameter that identifies which student photography is and the name of the photo. Normally, this would be the primary key of the table of StudentRecord.
Your table of StudentRecord, however, has no keys. It is a mistake. Each table must have a primary key. It does not appear that you have something that you could use as a natural key, we will create a synthetic key instead.
CREATE OR REPLACE TABLE StudentRecord ( StudentID NUMBER PRIMARY KEY, FirstName VARCHAR2(20) not null, LastName VARCHAR2(40) not null, HomeworkAvg NUMBER(6,2), AttendanceAvg NUMBER(6,2), ExamAvg NUMBER(6,2), StudentID VARCHAR2(10), Photograph BLOB, CONSTRAINT check_FirstName_LastName CHECK (FirstName != LastName) );
Once we do that, we can meaningfully define the procedure of the signature to the StudentID and the name of the image
CREATE OR REPLACE PROCEDURE load_blob_from_file (p_studentID StudentRecord.StudentID%TYPE, p_photo VARCHAR2)
Your UPDATE statement would then use the StudentID happened to determine which line to operate on a
UPDATE StudentRecord SET Photograph = empty_blob() WHERE StudentID = p_StudentID RETURN Photograph INTO b_photo;
That allows your procedure to work when there are multiple lines in the table of StudentRecord. It will make the new photo of an individual student to replace the old photo of this student only if there is.
Justin
Tags: Database
Similar Questions
-
How to insert data into tables using sql loops
Oracle 10.2 g
using Oracle sql * more
Table amounts is
nowcreate table student(id) as select distinct student_id from students_table;
Now create a sequencedesc student; will retrieve student ====== Name Null? Type =========================== ID VARCHAR2(10)
create sequence st_seq;
alter table student add column no;
This is the main partselect * from student no id ========= 234 298 ..........
There are 100 student in the table
now, I want to fill the table with sequences using seq.next_val
How to use a loop to insert 100 auto generated numbers in the table.
Thank you.
Expected result
Why I'm doing this instead ofSelect * from student no id ========= 1 234 2 298 .........
This will cause the duplication of the upncreate table student(no,id) as select st_se.nextval,student_id from students;
using separate throws an error.
Thank you.create table student(st_id,id) as select distinct st_seq.nextval,academicyear from student as select distinct st_seq.nextval,academicyear * ERROR at line 2: ORA-02287: sequence number not allowed here
Hello
As you discovered, you cannot use SELECT DISTINCT and sequence. NEXTVAL in the same query.
You can SELECT DISTINCT in a subquery, like this:
CREATE TABLE student AS WITH distinct_ids AS ( SELECT DISTINCT student_id AS id FROM students_table ) SELECT st_seq.NEXTVAL AS st_id , id FROM distinct_ids;
-
Insert row into table 2D retaining the previous values
Amendment of data entered in a 2D array and also displayed a table. When the time rolls 3 columns of data are held and then created line. Problem is the line moves downwards, but the previous data is not stored. I tried using shift register, but that only auto markings lines table to fill as the iteration of the loop. Looks like a basic really simple problem but going round in circles on this one there...
In this case, the function 'Replace table subset' is what is needed to replace the last values in line with the current values of the user interface.
On the evolution of the time, an addition occurs.
An update of the VI is attached.
-
compare the process with normal insert insertion and nested table inserts
Hi gurus,
I need to insert data block (in Millions of records) in my table. What is the best option
(1) normal insert
INSERT / * ADDING * / into table
Select * from table1
(2) declare
n_table table % rowtype;
n_table ins_table;
Start
Select * BULK collect into ins_table
FROM table1
forALL i in ins_table.first... ins_table.last
insert into table values (ins_table (i));
commit;
end;
Please notify. and comeout with valid arguments.
Thank you
S
If the destination table is empty, then you can do ETG (CREATE TABLE AS SELECT...). This would be by far the fastest way. But to choose one of the choices you gave, the top hand is the first.
The second option is a very bad idea. Collection of PL/SQL uses PGA. Millions of PGA data costs very expensive. And also this approach doesn't give not any kind of performance on simple INSERT INTO SELECT...
-
Select Insert in a table without duplicate line
Hi guys,.
I need to migrate the data from table A in the old schema in table B of the new scheme.
The two tables contain exactly a similar structure.
For example: table A have neck one and col b, table B col, col b
The two table has applied the unique constraint on A col, col B
Col B, col A
now, my intention is to insert, select into table B to table A.
If there is similar data in Table B, I want to keep the data in Table B.
Please, how do I design the query?A solution would be
insert into b (select * from a minus select * from b)
-
Read the BLOB and insert data into a table
Hi all
Let us examine below on Oracle DB 12 c:
create table xx_test3 (c blob); insert into xx_test3 (c) values (utl_raw.cast_to_raw( 'azertyuiop,qsdfghjklm,wxcvbn')); create table xx_target (col1 varchar2(50));
Can someone guide me how to read the data and insert it into the xx_target table?
Necessary result is:
select * from xx_target; COL1 -------------------------------------------------- azertyuiop qsdfghjklm wxcvbn 3 rows selected.
Thanks in advance,
Stoyanov.
insert into xx_target (col1)
with the data as)
Select utl_raw.cast_to_varchar2 (dbms_lob.substr (c, 32000, 1)) CBC
of xx_test3
)
Select regexp_substr (CBC, ' [^,] +', 1, level)
from the data
connect by level<= regexp_count(src,="" ',')="" +="">=>
-
Inserting blob in a table: problems
I wanted to insert a picture into the database using PLSQL table. Please let me know if the image file must be on a database server or I can insert it premises as well. Please find below my code and the error I get.
Call to the procedure and the error
My procedure to insert data into the tableBEGIN load_blob_from_file ( 'D:\PRoJeCt-RS\OracleError', 'item', 'item_blob', 'id', '2'); END; BEGIN * ERROR at line 1: ORA-22285: non-existent directory or file for FILEEXISTS operation ORA-06512: at "SYS.DBMS_LOB", line 504 ORA-06512: at "CMSRTPGM.LOAD_BLOB_FROM_FILE", line 18 ORA-06512: at line 2
The table structureCREATE OR REPLACE PROCEDURE load_blob_from_file ( src_file_name IN VARCHAR2 , table_name IN VARCHAR2 , column_name IN VARCHAR2 , primary_key_name IN VARCHAR2 , primary_key_value IN VARCHAR2 ) IS -- Define local variables for DBMS_LOB.LOADCLOBFROMFILE procedure. des_blob BLOB; src_blob BFILE := BFILENAME('GENERIC',src_file_name); des_offset NUMBER := 1; src_offset NUMBER := 1; -- Define a pre-reading size. src_blob_size NUMBER; -- Define local variable for Native Dynamic SQL. stmt VARCHAR2(2000); BEGIN -- Opening source file is a mandatory operation. IF dbms_lob.fileexists(src_blob) = 1 AND NOT dbms_lob.isopen(src_blob) = 1 THEN src_blob_size := dbms_lob.getlength(src_blob); dbms_lob.open(src_blob,DBMS_LOB.LOB_READONLY); END IF; -- Assign dynamic string to statement. -- We are going to obtain the clob locator (pointer) to use later stmt := 'UPDATE '||table_name||' ' || 'SET '||column_name||' = empty_blob() ' || 'WHERE '||primary_key_name||' = '||''''||primary_key_value||''' ' || 'RETURNING '||column_name||' INTO :locator'; -- Run dynamic statement. -- the locator (pointer) to the blob will assigned to des_blob EXECUTE IMMEDIATE stmt USING OUT des_blob; -- Read and write file to BLOB, close source file and commit. dbms_lob.loadblobfromfile( dest_lob => des_blob , src_bfile => src_blob , amount => dbms_lob.getlength(src_blob) , dest_offset => des_offset , src_offset => src_offset ); -- Close open source file. dbms_lob.close(src_blob); -- Commit write and conditionally acknowledge it. IF src_blob_size = dbms_lob.getlength(des_blob) THEN $IF $$DEBUG = 1 $THEN dbms_output.put_line('Success!'); $END ROLLBACK; ELSE $IF $$DEBUG = 1 $THEN dbms_output.put_line('Failure.'); $END RAISE dbms_lob.operation_failed; END IF; END load_blob_from_file;
Thanks in advance!CREATE TABLE ITEM ( "ID" NUMBER, "TITLE" VARCHAR2(50), "ITEM_BLOB" BLOB )
Concerning
34MCA2K2
Published by: BluShadow on June 14, 2012 13:31
addition of {noformat}{noformat} tags for readability. Please read {message:id=9360002} and learn to do this yourself.
Yes,
Start by creating an object directory point to a physical directory on the server database as the 'oracle '.
user has read/write permissions.CONN / AS SYSDBA
CREATE or REPLACE DIRECTORY nom_repertoire as 'D:\Applications ';
GRANT READ, WRITE ON DIRECTORY NOM_REPERTOIRE TO USER_NAME;Here is the diagram user USER_NAME.
Then,.
create and populate a table to contain the images in the database your user to user schema.and try this procedure...
DECLARE l_dir VARCHAR2(10) := 'DIR_NAME'; ----> Is the Directory Object we created. l_file VARCHAR2(20) := 'site_logo.gif'; ----- > The image to be located in the DB Server in the Directory Path DIR_NAME mentioned. l_bfile BFILE; l_blob BLOB; BEGIN INSERT INTO images (id, name, image) VALUES (images_seq.NEXTVAL, l_file, empty_blob()) RETURN image INTO l_blob; l_bfile := BFILENAME(l_dir, l_file); DBMS_LOB.fileopen(l_bfile, DBMS_LOB.file_readonly); DBMS_LOB.loadfromfile(l_blob, l_bfile, DBMS_LOB.getlength(l_bfile)); DBMS_LOB.fileclose(l_bfile); COMMIT; END;
Thank you
Shankar -
How to insert data into a BLOB column using sql
Hi all
How to insert data into the BLOB column directly using sql.
Thank youcreate table temp ( a blob, b clob); SQL> / Insert into temp values ('32aasdasdsdasdasd4e32','adsfbsdkjf') ; * ERROR at line 1: ORA-01465: invalid hex number Please help in this.
Prakash PInsert into temporary values (UTL_RAW. CAST_TO_RAW ('32aasdasdsdasdasd4e32'), 'adsfbsdkjf');
-
How to insert data into the BLOB column
Hi all
Can someone help me to insert data in the BLOB data type column?
The structure of the table is
CREATE TABLE XXATFL_DM_FORCAST_STG
(
TASK_ID NUMBER,
USER_ID NUMBER,
CREATED_BY NUMBER (15),
CREATION_DATE DATE,
LAST_UPDATED_BY NUMBER (15),
DATE OF LAST_UPDATE_DATE,
LAST_UPDATE_LOGIN NUMBER (15),
RECORD_STATUS VARCHAR2 (1 BYTE),
ERROR_MESSAGE VARCHAR2 (4000 BYTE),
DATA_FILE BLOB
)
I want to insert data into the column DATA_FILE. and this insert statement inside a procedure.
Please help me as soon as possible because it is very urgent for me
Thank you and best regards,
CharrierCharrier,
If you form the string yourself, you can use the function utl_raw.cast_to_raw on your channel.
http://download.Oracle.com/docs/CD/B12037_01/AppDev.101/b10802/u_raw.htm#997086
sql> create table t( 2 id number, 3 l_blob blob 4 ); sql> insert into t values(1, utl_raw.cast_to_raw('SampleString')); 1 row created. sql> commit; Commit complete.
-
Dear all,
Is it possible to insert the ring into Table?
I have a Table whose values are constant, but not fixed, for example, Col 1 can be set to 1 or 2.
I am attaching the picture of the table to show clearly what I want.
Collar "Site" can have values On or OFF.
Nervous collar can have the values 1, 2 or 3.
Col. Ampl can have values from Ridge to Ridge or Bs - PK.
Is it possible to make a table to store multiple values for a cell?
Thank you
Ritesh
Hi Mickaël
This link will give you full details about your query. If it works let me know.
http://forums.lavag.org/table-with-drop-down-items-t11249.html
-
Gears - error when you try to insert values into a table with multiple columns
Hello
I started playing with the gears and SQlLite today and I get an error when I try to insert values into a table with multiple columns.
I have:
var db = google.gears.factory.create('beta.database'); db.open('developerSet'); db.execute('create table if not exists Developers (DeveloperName text, DeveloperAge int)'); var devName = "Davy" var devAge = 32; try { db.execute('insert into Developers values (?, ?)', [devName, devAge]); alert('success'); } catch (e) { alert(e); }
I get the error:
net.rim.device.api.database.DatabaseException; insert into developers values (?,?): SQL logic error or missing database.
I use this reference: http://code.google.com/apis/gears/api_database.html
Everything works if I have only one field as:
var db = google.gears.factory.create('beta.database'); db.open('developerSet'); db.execute('create table if not exists Developers (DeveloperName text)'); var devName = "Davy" var devAge = 32; try { db.execute('insert into Developers values (?)', [devName]); alert('success'); } catch (e) { alert(e); }
I use the plug-in Visual Studio 2.0 for 2008 that are running Windows XP SP and Simulator 2.13.0.56
Thank you
Davy
Yes, a SQLite database will persist between battery pulls. The database is registered either to internal MEM or removable media (not the device memory), depending on which is available on your device.
In general, its not considered a best practice to remove your table as soon as it is empty and re - create it again when you want to add data. This adds extra overhead fresh for the final, delete and insert first for a given table. Instead, define and finalize your drawing before you create your table. Once created, review the static schema.
That being said, for development purposes, it may be easier to provide an easy way to drop your tables while you develop your schema.
See you soon,.
Adam
-
How to insert records into a table from a button?
Hey,.
I use Oracle form and I have a trigger button when button pressed.
I have a project being given for one of my classes. Basically, I have a Customers table and they hit the news. When they press submit, it must insert it into the table.
And here is the code I wrote to be inserted inside the button
declare
v_cust_id customer_detail.customer_id%type;
v_f_name customer_detail.fname%type;
v_l_name customer_detail.lname%type;
v_destination customer_detail.destination%type;
v_travel_date customer_detail.travel_date%type;
v_address customer_detail.address%type;
v_telephone customer_detail.telephone%type;
Start
insert into customer_detail
values (v_customer_id, v_fname, v_l_name, v_destination, v_travel_date, v_address, v_telephone);
end;
/
But its gives me an error. Is a PLsql Oracle form different syntax?
Why do you need to insert in the command, when applications oracle has this native features?
Make a form based on the table and just use
COMMIT_FORM; on your BUTTON.
Hope this helps
Hamid
-
Insert data into the source of destinator Table
Hi all
Need to insert data in sample_table1 table xxc_source_table sample_table2
create table xxc_source_table (DESCRIPTION varchar2 (10));
Insert the table xxc_source_table values('A201.) ABC.4084.GR');
create table sample_table1 (col_1 varchar2 (10), col_2 varchar2 (10), col_3 varchar2 (10), col_4 varchar2 (10));
create table sample_table2 (col_1 varchar2 (10), col_2 varchar2 (10), col_3 varchar2 (10), col_4 varchar2 (10), moved_flag varchar2 (2));
col_1 = A201
col_2 = ABC
col_3 = 4084
col_4 = GR
Note: Insert data into the col_1, the col_3, the col_4 of the xxc_source_table sample_table1
(2) if the next (form xxc_source_table) data is exist in the sample_table2, and then set the moved_flag as Y in this column
3) xxc_source_table has 17000 lines
Thank you.
Post edited by: Rajesh123 please do not consider cross the line message
Hi Renon,
Why you don't want substr and instr? For best performance, you should go with substr and instr instead of regexp_substr. However you asked me to provide the code instead of substr and InStr. Then try the below...
INSERT ALL
IN VALUES sample_table1 (col1, col2, col3, col4)
IN sample_table2 VALUES (col1, col2, col3, col4, 'Y')
SELECT REGEXP_SUBSTR (DESCRIPTION,'[^.] +', 1, 1) col1,.
REGEXP_SUBSTR (DESCRIPTION,'[^.] +', 1, 2) col2.
REGEXP_SUBSTR (DESCRIPTION,'[^.] +' 1, 3) col3.
REGEXP_SUBSTR (DESCRIPTION,'[^.] +' 1, 4) col4
OF xxc_source_table;
Thank you
Ann
-
inserting data into a table from another table
Hello
I have a to insert a data in the other table.
My requirement is I field Date_effect_date in the departments, I would copy the details field in dept_effect_date of employees.
I used the query
Insert in the dept_effect_date of certain employees (dept_effect_date) departments;
and the result is:
SQL error: ORA-01400: cannot insert NULL into ('HR'. "'"' EMPLOYEES'."" EMPLOYEE_ID')
01400 00000 - "impossible to insert a NULL value in (%s)."
MY DB: oracle 10g XE
Sainaba
You can do this by UPDATE not INSERT.
Sudheeryekkala wrote:
Hello
I have a to insert a data in the other table.
My requirement is I field Date_effect_date in the departments, I would copy the details field in dept_effect_date of employees.
I used the query
Insert in the dept_effect_date of certain employees (dept_effect_date) departments;
and the result is:
SQL error: ORA-01400: cannot insert NULL into ('HR'. "'"' EMPLOYEES'."" EMPLOYEE_ID')
01400 00000 - "impossible to insert a NULL value in (%s)."
MY DB: oracle 10g XE
Sainaba
INSERT the results of will by adding new lines to the table so you have the above error. In your case, you must update the value of the existing column
(or, if the volume is large, then fill the data -
join the table two in a new table CREATE TABLE EMP_NEW AS SELECT * FROM EMPLOYEES, DEPARTMENTS
. fall of
; Rename emp_new to
; * constraints/indexes if necessary be supported...
)
E employees update
Set e.dept_effect_date = (select d.dept_effect_date
departments d
where e.dept_id = d.dept_id);
Concerning
Biju
-
table name not valid error when inserting values into a table
I use the following statement to insert values into a table:
curs. Execute ("INSERT INTO _ * '%s' * _ VALUES ((SELECT MAX (REC_ID) + 1 OF GSAP_MSG_IN), (SELECT MAX (gsap_msg_id) + 1 OF GSAP_MSG_IN), 'SHELLSAP', sysdate, '%s', EMPTY_BLOB(), 1, SYSDATE, EMPTY_BLOB (), SYSDATE)" %(*table_name*,file_extension)) ")
whence table_name the following statement
table_name = ' config.staging_db_tablesNames ['in_msgs]
as I created a configuration file for all parameters that can change. The value of the table in the audit using a print command is correctly, but when put in the query above to run the insert statement gives an error. The following is the summary of comprehensive performance where you can see the table name as
$ python gsapscnr.py
Vote for the data files in/home/mh/inbox /...
GSAP_MSG_IN
Traceback (most recent call changed):
File "gsapscnr.py", line 147, in it?
poll_for_data()
File "gsapscnr.py", line 86, in poll_for_data
Sorter = load_details_first)
File "gsapscnr.py", line 42, survey
curs. Execute ("INSERT INTO '%s' VALUES ((SELECT MAX (REC_ID) + 1 OF GSAP_MSG_IN), (SELECT MAX (gsap_msg_id) + 1 OF GSAP_MSG_IN), 'SHELLSAP', sysdate, '%s', EMPTY_BLOB(), 1, SYSDATE, EMPTY_BLOB (), SYSDATE)" %(table_name,file_extension)) ")
cx_Oracle.DatabaseError: ORA-00903: invalid table name
Can anyone help with this problem please. I'm passing the value of the table in a bad way. Also if anyone can suggest a good tutorial for paythong programming using cx_Oracle.
ConcerningPrint the SQL string that you establish, cut and paste it this output in SQL * more and see if it runs. This may show you that you should remove the single quotes around the name of the table %s in the Python file.
Maybe you are looking for
-
I've successfully installed Adobe Flash Player, but I do not see it in m's list of Plug-Ins
According to the Adobe Test Page, I installed the plugin Flash Player successfully, but I don't see that the Flash Player listed in my plug-ins. I noticed that Adobe Reader and Adobe Shockwave Flash but not Adobe Flash Player. Why is it not this plug
-
Slate 21 - How to save apps on sd card...
Hi all Recently got my slate 21 and love him - it's a little amazing Kit, but... Is there a way to save apps to sd card, as you would on other devices? Also, even if I'm here, is it peripheral s still under development for? Be it get updated for the
-
Background: Service mobile organization using more than 100 different pieces of software among more than a dozen OEMs to program various electronic communications / equipment. Portable computers used in the field all have standard RS-232 ports of se
-
What is the program of intellimirror?
What is intellimirror program and it is something that would need the user for personal use and not for commercial purposes?
-
Alienware Area 51 ALX difficult questions of car upgrade
Hi all! I have an area 51 alx from ~ 2010 with a card mother stock. I'm looking to replace the disks accompanying with a Samsung 850 Evo SSD. SSD SATA3 and I'm fairly certain that the parent stock on my machine has only SATA 2 correct slots? My qu