如何在blob字段中进行文本搜索

dtcbnfnu  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(369)

我们创建了一个blob类型的列来存储负载的6000到7000个ascii字符。我使用以下代码插入有效负载。

PreparedStatement stmt=conn.prepareStatement(insertQuery);
String record="My ASCII Payload";
stmt.setBytes(1, record.getBytes());
stmt.execute();

当我运行下面的查询时,我可以看到输出中字符的长度。

select dbms_lob.getLength(MY_PAYLOAD)
  from RAW_DATA;

6085

我想在这个blob列中执行文本搜索,我尝试了以下选项没有任何效果。在blob列中执行文本搜索的正确方法是什么?

SELECT * FROM RAW_DATA t
WHERE dbms_lob.instr(t.MY_PAYLOAD,utl_raw.cast_to_raw(‘1234’))>0

select *
       from RAW_DATA 
       where dbms_lob.instr (MY_PAYLOAD, -- the blob
                       utl_raw.cast_to_raw ('1234'),
                       1, -- where to start. i.e. offset
                       1 -- Which occurrance i.e. 1=first
                        ) > 0
o3imoua4

o3imoua41#

您可以在blob上创建文本索引,只要它们包含文本(自然)

SQL> create table t ( x int, b blob);

Table created.

SQL>
SQL> insert into t values ( 1, utl_raw.cast_to_Raw('Hello there'));

1 row created.

SQL> insert into t values ( 2, utl_raw.cast_to_Raw('Goodbye tomorrow'));

1 row created.

SQL>
SQL> create index t_ix on t ( b )
  2  indextype is ctxsys.context;

Index created.

SQL>
SQL> select x
  2  from t where contains(b,'Hello') > 0;

         X
----------
         1

SQL>
SQL> select utl_raw.cast_to_varchar2(b)
  2  from t
  3  where contains(b,'Hello') > 0;

UTL_RAW.CAST_TO_VARCHAR2(B)
---------------------------------------------------------------------------------
Hello there

相关问题