Home php教程 php手册 用PHP操纵Oracle的LOB类型的数据

用PHP操纵Oracle的LOB类型的数据

Jun 13, 2016 am 10:20 AM
oracle php manipulate Tutorial data article have source use of Know type computer

文章来源:IT计算机教程

用过Oracle的人都知道,Oracle有一种数据类型叫VARCHAR2,用来表示不定长的字符串。VARCHAR2也是Oracle公司推荐使用的类型。但使用VARCHAR2有个问题:最大只能表示4000个字符,也就相当于2000个汉字。如果你的程序中某个字符的值要大于20002个汉字,用VARCHAR2就不能满足要求了。这时候,你有两个选择,一是用多个VARCHAR2来表示,二是用LOB字段。这里我们来看看第二个办法。

  先来大体了解一下Oracle的LOB字段。Oracle的LOB类型分为三种:BLOB,CLOB和BFILE。CLOB称为字符LOB,BLOB和BFILE是用来存储二进制数据的。CLOB和BLOB的最大长度是4GB,它们把值存放在Oracle数据库中。BFILE和BLOB类似,但它把数据放在外部的文件中,所以它又称为外部BLOB(External BLOB)。

  我想,我们对MYSQL应该都不会陌生。MYSQL中也有类似的数据类型,如TEXT和BLOB。在PHP的MYSQL函数中,对TEXT/BLOB的操作是直接的,就象其它类型的数据一样。但在Oracle中,情况就不一样了。Oracle把LOB当作一种特殊的数据类型来处理,在操作上不能用常规的方法。比如,不能在INSERT语句中直接把值插入到LOB字段中,也不能用LIKE进行查找。

  下面就通过几个例子来说明如何用PHP的OCI函数来插入,取出和查询LOB数据。

  插入

  不能直接用INSERT语句向LOB字段中插入值。一般情况下,有如下的几步:

  1、先分析一个INSERT语句,返回一个LOB的描述符

  2、用OCI函数生成一个本地的LOB对象

  3、将LOB对象绑定到LOB描述符上

  4、执行INSERT语句

  5、给LOB对象赋值

  6、释放LOB对象和SQL语句句柄

  下面的这个例子是把用户上传的图片文件存放到BLOB(或BFILE中,操作稍有不同)中。首先要建一个表,结构如下:

CREATE TABLE PICTURES (
ID NUMBER,
DESCRIPTION VARCHAR2(100),
MIME VARCHAR2(128),
PICTURE BLOB
);

  如果要实现ID的自动增加,再建一个SEQUENCE:

CREATE SEQUENCE PIC_SEQ;

  然后是用来处理数据的PHP程序代码。

<?php

 //建立Oracle数据库连接

 $conn = OCILogon($user, $password, $SID);

 //提交SQL语句给Oracle
 //在这里要注意的两点:一是用EMPTY_BLOB()函数。这是Oracle的内部函数,返回一个LOB的定位符。在插入LOB时,只能用这个办法先生成一个空的LOB定位符,然后对这个定位符进行操作。EMPTY_BLOB()函数是针对BLOB类型的,对应于CLOB的是EMPTY_CLOB()。二是RETURNING后面的部分,把picture返回,让PHP的OCI函数能够处理。

 $stmt = OCIParse($conn,"INSERT INTO PICTURES (id, description, picture)
VALUES (pic_seq.NEXTVAL, $description, $lob_upload_type, EMPTY_BLOB()) RETURNING picture INTO :PICTURE");

 //生成一个本地LOB对象的描述符。注意函数的第二个参数:OCI_D_LOB,表示生成一个LOB对象。其它可能的还有OCI_D_FILE和OCI_D_ROWID,分别对应于BFILE和ROWID对象。

 $lob = OCINewDescriptor($conn, OCI_D_LOB);

 //将生成的LOB对象绑定到前面SQL语句返回的定位符上。

 OCIBindByName($stmt, :PICTURE, &$lob, -1, OCI_B_BLOB);
 OCIExecute($stmt);

 //向LOB对象中存入数据。因为这里的源数据是一个文件,所以直接用LOB对象的savefile()方法。LOB对象的其它方法还有:save()和load(),分别用来保存和取出数据。但BFILE类型只有一个方法就是save()

 if($lob->savefile($lob_upload)){
  OCICommit($conn);
  echo "上传成功<br>";
 }else{
  echo "上传失败<br>";
 }

 //释放LOB对象

 OCIFreeDesc($lob);
 OCIFreeStatement($stmt);
 OCILogoff($conn);
?>


  还有一个要注意的地方:LOB字段的值最少要1个字符,所以在save()或savefile()之前,要确保值不能为空。否则,Oracle会出错。

取出

  对一个LOB中取出数据,有两种办法。一是生成一个LOB对象,然后绑定到一条SELECT语句返回的定位符上,再用LOB对象的load()方法取出数据;二是直接用PHP的OCIFetch***函数。第一种方法比第二种方法要麻烦得多,所以我直接说说第二种方法。

  还是用上面的表。

<?php
 $conn = OCILogon($user, $password, $SID);
 $stmt = OCIParse($conn,"SELECT * FROM PICTURES WHERE ID=$pictureid");
 OCIExecute($stmt);

 //秘密就在PCIFetchInfo的第三个参数上:OCI_RETURN_LOBS。第三个参数是FETCH的模式,如果OCI_RETURN_LOBS,就直接把LOB的值放到结果数组中,而不是LOB定位符,也就不用LOB对象的load()方法了。

 if (OCIFetchInto($stmt, $result, OCI_ASSOC+OCI_RETURN_LOBS))
 {
  echo "Content-type: " . StripSlashes($result[MIME]);
  echo StripSlashes($result[PICTURE]);
 }
 OCIFreeStatement($stmt);
 OCILogoff($conn);
?>

  这个程序用来显示放在LOB中的数据(图片)。调用方法(假设脚本名是getpicture.php):

<IMG SRC="getpicture.php?pictureid=99" ALT="放在Oracle LOB中的图片">

  查询

  前面已经提了下,对于Oracle的LOB字段是不能用LIKE进行匹配的。怎么办呢?其实并不复杂,Oracle有一个匿名的程序包,叫DBMS_LOB,里面有所有的操作LOB所需的过程。

  假设有象这样一个表:

CREATE TABLE ARTICLES (
 ID NUMBER,
 TITLE VARCHAR2(100),
 CONTENT CLOB
);

  文章的内容放在CONTENT字段中。

  现在我们要找出所以内容中包含"PHP中文用户"的文章,可以这么来做:

<?php
 $conn = OCILogon($user, $password, $SID);

 //WHERE子句中用了DBMS_LOB.INSTR过程。它有四个参数,前面两个分别表示LOB的定位符(可以直接用字段表示)和要查找的字符串;后面两个分别表示开始的偏移量和出现的次数。要注意的是必须判断它的返回值,也就是要大于0。

 $stmt = OCIParse($conn,"SELECT * FROM ARTICLES WHERE DBMS_LOB.INSTR(CONTENT, PHP中文用户, 1, 1) > 0");
 OCIExecute($stmt);
 if (OCIFetchInto($stmt, $result, OCI_ASSOC+OCI_RETURN_LOBS))
 {
  ...
 }
 OCIFreeStatement($stmt);
 OCILogoff($conn);
?>

  Oracle还提供了许多用来操作LOB数据的过程,如LENGTH, SUBSTR等等。至于它们的详细用法,可以考虑Oracle的开发手册。

  关于对Oracle数据库中的LOB类型的数据的操作,就说这么多了。由于我接触Oracle时间不长,本文中可能还会有错误,欢迎大家批评指正。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to add and replace hard disks in oracle rac How to add and replace hard disks in oracle rac Apr 11, 2025 pm 05:39 PM

Oracle RAC hard disk new and replacement operations: Add hard disk: Add new disks, create ASM disk groups, add to clusters, move data files. Replace hard disk: Identify the failed hard disk, close the disk group, replace the hard disk, reopen the disk group, repair the failed disk, and move the data files.

How to deal with oracle garbled code How to deal with oracle garbled code Apr 11, 2025 pm 07:00 PM

Oracle garbled problems are usually caused by improper character set settings. Solutions include: Checking the server, database, and client character sets. Set up the server, database, and client character sets as needed. Use the CONVERT function or the DBMS_LOB.CONVERT_LOB function to fix garbled data. Always specify the character set and set the NLS parameters correctly.

How to re-query oracle How to re-query oracle Apr 11, 2025 pm 07:33 PM

Oracle provides multiple deduplication query methods: The DISTINCT keyword returns a unique value for each column. The GROUP BY clause groups the results and returns a non-repetitive value for each group. The UNIQUE keyword is used to create an index containing only unique rows, and querying the index will automatically deduplicate. The ROW_NUMBER() function assigns unique numbers and filters out results that contain only line 1. The MIN() or MAX() function returns non-repetitive values ​​of a numeric column. The INTERSECT operator returns the common values ​​of the two result sets (no duplicates).

How to check tablespace size of oracle How to check tablespace size of oracle Apr 11, 2025 pm 08:15 PM

To query the Oracle tablespace size, follow the following steps: Determine the tablespace name by running the query: SELECT tablespace_name FROM dba_tablespaces; Query the tablespace size by running the query: SELECT sum(bytes) AS total_size, sum(bytes_free) AS available_space, sum(bytes) - sum(bytes_free) AS used_space FROM dba_data_files WHERE tablespace_

How to connect to a cloud server How to connect to a cloud server Apr 11, 2025 pm 06:51 PM

The steps to connect to a cloud server through an Oracle client are as follows: Create an SSH key and copy the public key to the cloud server. Configure the Oracle client and add the connection information of the cloud server to the tnsnames.ora file. Create a new database connection in the Oracle client, enter the username, password, and DSN. Click OK and verify that the connection is successful.

Summary of basic knowledge of oracle database Summary of basic knowledge of oracle database Apr 11, 2025 pm 06:33 PM

Oracle Database is a reliable, scalable and feature-rich relational database management system (RDBMS). Its architecture follows the client-server model, including server-side components (Oracle Net), instances, shared memory areas (SGAs), background processes, and database files that store data. Basic concepts include tables, rows, columns, primary keys, foreign keys, indexes and cursors. The database is known for its advantages such as high availability, big data support, rich features, strong security and ease of use.

How to modify oracle character set How to modify oracle character set Apr 11, 2025 pm 06:57 PM

To modify the Oracle character set, you need to: back up the database; modify the character set settings in the init.ora file; restart the database; modify existing tables and columns to use the new character set; reload the data; modify the database link (optional).

How to use stored procedures for oracle How to use stored procedures for oracle Apr 11, 2025 pm 07:03 PM

A stored procedure is a set of SQL statements that can be stored in a database and can be called repeatedly as a separate unit. They can accept parameters (IN, OUT, INOUT) and provide the advantages of code reuse, security, performance and modularity. Example: Create a stored procedure calculate_sum to calculate the sum of two numbers and store them in the OUT parameter.

See all articles