Oracle - An Managing Lob examples

本文详细介绍了如何使用PL/SQL从计算机文件系统读取文件,并将其作为CLOB类型插入到Oracle数据库中。通过创建表、使用Oracle数据库系统包和外部表技术,实现文件跨系统加载功能,以及利用SQL*Loader工具进行高效的数据加载。

I met a request: I need to read files from Computer File System and insert this readed files into Oracle database as Clob, and need to complete this through PL/SQL only.

To describ this request more clearly, we list the create table sql statements firstly:

create table stylesheet (
  stylesheet_name varchar2(100) primary key not null,
  stylesheet_text CLOB
);

Obviously, this is a very simple table, which has 2 column, stylesheet_name is table primary key has not null constraints, and stylesheet_text is a clob type use to store the files readed from Computer file system.

 

First: I found use Oracle External Tables skill can meet this request, But have some defects:

In this condition, we use Oracle Databse System Package DBMS_LOB, DBMS_LOB has some methods can handle this. DBMS_LOB.loadfromfile() can insert the clob values to database. So we need to read the file system's data to clob pattern, use BFILE can accomplish this, the whole processing SQL CODE as following:

CREATE OR REPLACE DIRECTORY 
  XSLT_LOB_DIR
  AS
  'C:/Share/xslt'
/

DECLARE
  dest_clob   CLOB;
  src_bfile    BFILE  := BFILENAME('XSLT_LOB_DIR', 'MSProject2Plan.xslt');
BEGIN
  insert into stylesheet(stylesheet_name, stylesheet_text) values('MSProject2Plan', empty_clob()) returning stylesheet_text into dest_clob;
  DBMS_LOB.fileopen(src_bfile);
  DBMS_LOB.loadfromfile(dest_clob, src_bfile, DBMS_LOB.getlength(src_bfile));
  DBMS_LOB.fileclose(src_bfile);
  commit;
END;
/  

 'MSProject2Plan.xslt' is the file we need to insert into table stylesheet, which saved under 'C:/Share/xslt' folder.

Use the above can sovle the Request only if this files and Oracle Database Syatem in the same machine, if you try this in Oracle client or other client the following error you will found :

ORA-22288: file or LOB operation FILEOPEN failed
The system cannot find the path specified.

 So this solution isn't able to meet our request,  we need to find another solution:

 

Second: Use Oracle utility to load the file from Computer File System

      SQL*Loader (sqlldr ) is the utility to use for high performance data loads.  The data can be loaded from any text file and inserted into the database. The key feature what sqlldr can meet our request is : It Can Load data across a network. This means that you can run the SQL*Loader client on a different system from the one that is running the SQL*Loader server.

1. SQL*Loader architecture



 The below figure depicts the SQL*Loader architecture.  SQL*Loader reads a data file and a description of the data which is defined in the control file.  Using this information and any additional specified parameters (either on the command line or in the PARFILE), SQL*Loader loads the data into the database. 

2. The Test Sequence(a demo use SQL*Loader load data)



 Also as the below figure depicted, My Test Example use a windows scrpits invoke test.sql, test.sql invoke the stylesheetcontrol.sql to complete the load processing.

3. Test Demo List:

run.cmd

@echo off

set SYSTEMUSER=system
set SYSTEMPASSWD=ADMIN
set DBPROTOCOL=TCP
set DBMODE=DEDICATED
set COMUSR=IPCUSERTEST
set COMPASSWD=tibco
set DBSERVER=192.168.68.116
set DBPORT=1521
set SERVICENAME=orcl
set COMDIR=c:/
set TABLESPACENAME=IPCTEST

set SYSTEMCONNECTSTRING=%SYSTEMUSER%/%SYSTEMPASSWD%@(DESCRIPTION=(ADDRESS=(PROTOCOL=%DBPROTOCOL%)(HOST=%DBSERVER%)(PORT=%DBPORT%))(CONNECT_DATA=(SERVER=%DBMODE%)(SERVICE_NAME=%SERVICENAME%)))
set IPCUSERCONNECTSTRING=%COMUSR%/%COMPASSWD%@(DESCRIPTION=(ADDRESS=(PROTOCOL=%DBPROTOCOL%)(HOST=%DBSERVER%)(PORT=%DBPORT%))(CONNECT_DATA=(SERVER=%DBMODE%)(SERVICE_NAME=%SERVICENAME%)))
set SQLLDRCONNECTSTRING=%COMUSR%/%COMPASSWD%@//%DBSERVER%:%DBPORT%/%SERVICENAME%

rem echo %SYSTEMCONNECTSTRING%
rem echo %SQLLDRCONNECTSTRING%
echo sql*loader load data start...
echo.

set RUN_SCRIPT=%ORACLE_HOME%/bin/sqlplus -l -s "%SYSTEMCONNECTSTRING%" @test.sql %SQLLDRCONNECTSTRING% %IPCUSERCONNECTSTRING%

%RUN_SCRIPT%

 test.sql

define SQLLDRCONNECTSTRING='&1'

connect &IPCUSERCONNECTSTRING 
 
@stylesheetcontrol.sql &SQLLDRCONNECTSTRING

 stylesheetcontrol.sql

define SQLLDRCONNECTSTRING='&1'

set serveroutput on

DECLARE
BEGIN
  DBMS_OUTPUT.PUT_LINE('Create stylesheet table, Insert the MSProject2Plan.xslt into the stylesheet');
END;
/

create table stylesheet (
  stylesheet_name varchar2(100) primary key not null,
  stylesheet_text CLOB
);

host sqlldr &SQLLDRCONNECTSTRING control=loadXSLT.ctl log=logs/loadXSLT.log

 loadXSLT.ctl

LOAD DATA
INFILE xsltlist.dat
INTO TABLE stylesheet
REPLACE
FIELDS TERMINATED BY ','
( stylesheet_name CHAR(100),
  clob_filename   FILLER CHAR(100),
  stylesheet_text LOBFILE(clob_filename) TERMINATED BY EOF
)

 xsltlist.dat

MSProject2Plan,../xslt/MSProject2Plan.xslt

 Run the run.cmd the file under xslt folder(MSProject2Plan.xslt) will be loaded and insert into the table stylesheet. 
 

【四轴飞行器】非线性三自由度四轴飞行器模拟器研究(Matlab代码实现)内容概要:本文围绕非线性三自由度四轴飞行器模拟器的研究展开,重点介绍了基于Matlab的建模与仿真方法。通过对四轴飞行器的动力学特性进行分析,构建了非线性状态空间模型,并实现了姿态与位置的动态模拟。研究涵盖了飞行器运动方程的建立、控制系统设计及数值仿真验证等环节,突出非线性系统的精确建模与仿真优势,有助于深入理解飞行器在复杂工况下的行为特征。此外,文中还提到了多种配套技术如PID控制、状态估计与路径规划等,展示了Matlab在航空航天仿真中的综合应用能力。; 适合人群:具备一定自动控制理论基础和Matlab编程能力的高校学生、科研人员及从事无人机系统开发的工程技术人员,尤其适合研究生及以上层次的研究者。; 使用场景及目标:①用于四轴飞行器控制系统的设计与验证,支持算法快速原型开发;②作为教学工具帮助理解非线性动力学系统建模与仿真过程;③支撑科研项目中对飞行器姿态控制、轨迹跟踪等问题的深入研究; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,重点关注动力学建模与控制模块的实现细节,同时可延伸学习文档中提及的PID控制、状态估计等相关技术内容,以全面提升系统仿真与分析能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值