Oracle9i PHP 操作 CLOB字段

xiaoxiao2021-03-01  22

场景描述:

将oracle11g的含clob字段的数据抽取到oracle9i,发现oracle9i对clob的支持没11g的强大,导致php进行入库操作时报错

从网上找到的解决办法,每行记录包含一个clob字段比较好解决,但是包含多个clob字段,不太好解决,目前没找到好的解决办法,、

以下是从网上查到的资料

1. 单个clob操作,转载地址:http://www.cnblogs.com/jiafan/archive/2008/06/23/1228300.html

如果接触过mysql的话一定对mysql的text和blob不会陌生,在mysql中是直接操作text和blob的;

但是在oracle中,它把lob做为一种特殊的字段,不能直接进行操作--比如不能直接插入到lob字段中,也不能用like查询等等。不能直接用INSERT语句向LOB字段中插入值。一般情况下,有如下的几步: 1 先分析一个INSERT语句,返回一个LOB的描述符 2 用OCI函数<nobr id="clickeyekey0" style="color:rgb(102,0,255); border-bottom-color:rgb(102,0,255); border-bottom-width:1px; border-bottom-style:dotted; background-color:transparent; text-decoration:underline">生成</nobr>一个本地的LOB对象 3 将LOB对象绑定到LOB描述符上 4 执行INSERT语句 5 给LOB对象赋值 6 释放LOB对象和SQL语句句柄/* //(tabel_name:article)DDL; //just for test; //create table article(id number(11),content clob); */ //----------This is insert test---------------------------------- $conn = @OCILogon("YourUsername","YourPassword","YourDatabase"); $stmt = @OCIParse($conn,"insert into article values(1,EMPTY_CLOB()) RETURNING content INTO:CONTENT"); $clob = @OCINewDescriptor($conn,OCI_D_LOB);OCIBindByName($stmt,':CONTENT',&$clob,-1,OCI_B_CLOB); if(!OCIExecute($stmt, OCI_DEFAULT)) {print_r(OCIError($stmt));} if($clob->save($CONT)) //把要入库的变量初始化到这里$CONT,看半天才看懂,哎,惭愧 { OCICommit($conn); }else{ print_r(OCIError($stmt)); } //---------------Insert end----------------------------------------- //---------------Select start--------------------------------------- $sql = "select content from article order by id desc"; $stmt = @OCIParse($conn,$sql); @OCIExecute($stmt,OCI_DEFAULT); @OCIFetchInto($stmt,&$rows,OCI_RETURN_LOBS); echo "<br>Content is:\"".$rows[0]."\"";

//---------------Select end---------------------------------------

2. 同时操作多个字段,转载地址:http://cshongyin.blog.163.com/blog/static/16818875920123159524908/

在php使用了oci接口连接oracle,结果发现插入一组数据的时候不能同时插入多个clob变量,

PHP:OCI :Manue中给出的例子是:

<?php//Beforerunning,createthetable: //CREATETABLEmytab(mykeyNUMBER,myclobCLOB);$conn=oci_connect('hr','welcome','localhost/XE'); if(!$conn){$e=oci_error();trigger_error(htmlentities($e['message']),E_USER_ERROR); }$mykey=12343;//arbitrarykeyforthisexample;$sql="INSERTINTOmytab(mykey,myclob) VALUES(:mykey,EMPTY_CLOB()) RETURNINGmyclobINTO:myclob";$stid=oci_parse($conn,$sql);$clob=oci_new_descriptor($conn,OCI_D_LOB);oci_bind_by_name($stid,":mykey",$mykey,5);oci_bind_by_name($stid,":myclob",$clob,-1,OCI_B_CLOB);oci_execute($stid,OCI_DEFAULT);$clob->save("Averylongstring");oci_commit($conn);//FetchingCLOBdata$query='SELECTmyclobFROMmytabWHEREmykey=:mykey';$stid=oci_parse($conn,$query);oci_bind_by_name($stid,":mykey",$mykey,5);oci_execute($stid); print'<tableborder="1">'; while($row=oci_fetch_array($stid,OCI_ASSOC)){$result=$row['MYCLOB']->load(); print'<tr><td>'.$result.'</td></tr>'; } print'</table>';?>

但是,插入多个clob的情况没有说,查了好久也没查出什么好的方法,最后通过用update解决了,虽然解决的不是很好,还以上面情况来说 <?php//Beforerunning,createthetable: //CREATETABLEmytab(mykeyNUMBER,myclobCLOB, mycc CLOB);$conn=oci_connect('hr','welcome','localhost/XE'); if(!$conn){$e=oci_error();trigger_error(htmlentities($e['message']),E_USER_ERROR); }$mykey=12343;//arbitrarykeyforthisexample;$sql="INSERTINTOmytab(mykey,myclob, mycc) VALUES(:mykey,EMPTY_CLOB(), EMPTY_CLOB()) RETURNINGmyclobINTO:myclob";$stid=oci_parse($conn,$sql);$clob=oci_new_descriptor($conn,OCI_D_LOB);oci_bind_by_name($stid,":mykey",$mykey,5);oci_bind_by_name($stid,":myclob",$clob,-1,OCI_B_CLOB);oci_execute($stid,OCI_DEFAULT);$clob->save("Averylongstring1");oci_commit($conn); $sql="UPDATE mytab set mycc = EMPTY_CLOB() WHERE mykey = :mykey RETURNINGmyccINTO:mycc";$stid=oci_parse($conn,$sql);$clob=oci_new_descriptor($conn,OCI_D_LOB);oci_bind_by_name($stid,":mycc",$clob,-1,OCI_B_CLOB);oci_execute($stid,OCI_DEFAULT);$clob->save("Averylongstring2");oci_commit($conn); //FetchingCLOBdata$query='SELECTmyclob, mycc FROMmytabWHEREmykey=:mykey';$stid=oci_parse($conn,$query);oci_bind_by_name($stid,":mykey",$mykey,5);oci_execute($stid); print'<tableborder="1">'; while($row=oci_fetch_array($stid,OCI_ASSOC)){$result=$row['MYCLOB']->load(); print'<tr><td>'.$result.'</td>'; $result=$row['MYCC']->load(); print'<td>'.$result.'</td></tr>'; } print'</table>';?> 虽然解决了,但是如果有n个CLOB,就需要n次update,显然效率非常低,期待好的做法

转载请注明原文地址: https://www.6miu.com/read-3650231.html

最新回复(0)