1
1
.. _lobdata :
2
2
3
- ************************
4
- Using CLOB and BLOB Data
5
- ************************
3
+ ***************************************
4
+ Using CLOB, BLOB, NCLOB, and BFILE Data
5
+ ***************************************
6
6
7
7
Oracle Database uses :ref: `LOB objects <lobobj >` to store large data such as
8
8
text, images, videos, and other multimedia formats. The maximum size of a LOB
@@ -11,21 +11,20 @@ text, images, videos, and other multimedia formats. The maximum size of a LOB
11
11
There are `four types of LOBs <https://www.oracle.com/pls/topic/lookup?ctx=
12
12
dblatest&id=GUID-0A692C1B-1C95-4121-8F95-25BE465B87F6> `__:
13
13
14
- * BLOB - Binary Large Object, used for storing binary data. python-oracledb
15
- uses the type :attr: `oracledb.DB_TYPE_BLOB `.
16
14
* CLOB - Character Large Object, used for storing strings in the database
17
15
character set format. python-oracledb uses the type
18
16
:attr: `oracledb.DB_TYPE_CLOB `.
17
+ * BLOB - Binary Large Object, used for storing binary data. python-oracledb
18
+ uses the type :attr: `oracledb.DB_TYPE_BLOB `.
19
19
* NCLOB - National Character Large Object, used for storing strings in the
20
20
national character set format. python-oracledb uses the type
21
21
:attr: `oracledb.DB_TYPE_NCLOB `.
22
22
* BFILE - External Binary File, used for referencing a file stored on the
23
23
host operating system outside of the database. python-oracledb uses the
24
- type :attr: `oracledb.DB_TYPE_BFILE `. See `BFILEs <https://www.oracle.com
25
- /pls/topic/lookup?ctx=dblatest&id=GUID-D4642C92-F343-4700-9F1F-
26
- 486F82249FB8> `__ for more information.
24
+ type :attr: `oracledb.DB_TYPE_BFILE `.
27
25
28
- LOBs can be streamed to, and from, Oracle Database.
26
+ LOBs can be permanent or temporary. They can be inserted into, and fetched
27
+ from, Oracle Database in chunks, as mecessary.
29
28
30
29
LOBs up to 1 GB in length can be also be handled directly as strings or bytes
31
30
in python-oracledb. This makes LOBs easy to work with, and has significant
@@ -36,7 +35,7 @@ See `GitHub <https://github.com/oracle/python-oracledb/tree/main/samples>`__
36
35
for LOB examples.
37
36
38
37
Simple Insertion of LOBs
39
- ------------------------
38
+ ========================
40
39
41
40
Consider a table with CLOB and BLOB columns:
42
41
@@ -69,7 +68,7 @@ Note that with this approach, LOB data is limited to 1 GB in size.
69
68
.. _directlobs :
70
69
71
70
Fetching LOBs as Strings and Bytes
72
- ----------------------------------
71
+ ==================================
73
72
74
73
CLOBs and BLOBs smaller than 1 GB can queried from the database directly as
75
74
strings and bytes. This can be much faster than streaming a :ref: `LOB Object
@@ -121,7 +120,7 @@ handler:
121
120
connection.outputtypehandler = output_type_handler
122
121
123
122
Streaming LOBs (Read)
124
- ---------------------
123
+ =====================
125
124
126
125
Without setting ``oracledb.defaults.fetch_lobs `` to False, or without using an
127
126
output type handler, the CLOB and BLOB values are fetched as :ref: `LOB
@@ -168,7 +167,7 @@ repeatedly until all of the data has been read, as shown below:
168
167
169
168
170
169
Streaming LOBs (Write)
171
- ----------------------
170
+ ======================
172
171
173
172
If a row containing a LOB is being inserted or updated, and the quantity of
174
173
data that is to be inserted or updated cannot fit in a single block of data,
@@ -196,9 +195,8 @@ in the following code:
196
195
offset += len (data)
197
196
connection.commit()
198
197
199
-
200
198
Temporary LOBs
201
- --------------
199
+ ==============
202
200
203
201
All of the examples shown thus far have made use of permanent LOBs. These are
204
202
LOBs that are stored in the database. Oracle also supports temporary LOBs that
@@ -214,3 +212,101 @@ procedure exceeds that which can fit in a single block of data, however, you
214
212
can use the method :meth: `Connection.createlob() ` to create a temporary LOB.
215
213
This LOB can then be read and written just like in the examples shown above for
216
214
persistent LOBs.
215
+
216
+ .. _bfiles :
217
+
218
+ Using BFILEs
219
+ ============
220
+
221
+ `BFILEs <https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-D4642C92
222
+ -F343-4700-9F1F-486F82249FB8> `__ are objects stored in a directory in the
223
+ Oracle Database server file system, not in the database. The database column of
224
+ type BFILE stores a reference to this external binary file. Each BFILE column
225
+ can reference a single external file. BFILEs are read-only data types and
226
+ hence you cannot modify the file from within your application.
227
+
228
+ Before using the BFILE data type, you must:
229
+
230
+ - Create a `DIRECTORY <https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-
231
+ F7440C27-C7F0-4874-8C3C-F3BC1534CBE0> `__ object which is an alias for the
232
+ full path to the directory containing BFILE data in the database server file
233
+ system. For example, you can create a DIRECTORY object by using:
234
+
235
+ .. code-block :: sql
236
+
237
+ create or replace directory my_bfile_dir as '/demo/bfiles'
238
+
239
+ In the above example, "my_bfile_dir" is the directory alias.
240
+ "/demo/bfiles" is the physical directory in the database server file
241
+ system that contains the files. It is a string containing the full path name
242
+ of the directory and follows the operating system rules.
243
+
244
+ To allow non-privileged users to access this directory, you can grant access
245
+ using:
246
+
247
+ .. code-block :: sql
248
+
249
+ grant read on directory my_bfile_dir to hr;
250
+
251
+ Ensure that the Oracle Server processes have read access to the directory.
252
+
253
+ - Store the physical binary file in the directory in the database server file
254
+ system. For example, the binary file "my_bfile.txt" is stored in the
255
+ directory "/demo/bfiles".
256
+
257
+ Consider the file, "/demo/bfiles/my_bfile.txt", exists on the server and
258
+ contains the text, "This is my BFILE data". You can access the "my_bfile.txt"
259
+ file as detailed below.
260
+
261
+ The following table will be used in the subsequent examples.
262
+
263
+ .. code-block :: sql
264
+
265
+ create table bfile_tbl(
266
+ id number,
267
+ bfile_data bfile
268
+ );
269
+
270
+ **Inserting BFILEs **
271
+
272
+ You must use the `BFILENAME <https://www.oracle.com/pls/topic/lookup?ctx=
273
+ dblatest&id=GUID-1F767077-7C26-4962-9833-1433F1749621> `__ function in an INSERT
274
+ statement to associate a file and a BFILE column. The ``BFILENAME `` function
275
+ takes two arguments, the directory alias and the file name. To insert a BFILE
276
+ reference, for example:
277
+
278
+ .. code-block :: python
279
+
280
+ cursor.execute("""
281
+ insert into bfile_tbl (id, bfile_data) values
282
+ (:id, bfilename(:bfiledir, :bfilename))""" ,
283
+ id = 102 , bfiledir = " my_bfile_dir" , bfilename = " my_bfile.txt" )
284
+
285
+ connection.commit()
286
+
287
+ This inserts a reference to the file "my_bfile.txt" located in the directory
288
+ referenced by the alias "my_bfile_dir" into the bfile_tbl table.
289
+
290
+ **Fetching BFILEs **
291
+
292
+ To query the bfile_tbl table and fetch the BFILE LOB locator, you can use
293
+ the `BFILENAME <https://www.oracle.com/pls/topic/lookup?ctx=dblatest&id=GUID-
294
+ 1F767077-7C26-4962-9833-1433F1749621> `__ function as shown below:
295
+
296
+ .. code-block :: python
297
+
298
+ cursor.execute(" select bfilename(:bfiledir, :bfilename) from bfile_tbl where id = :id" ,
299
+ id = 102 , bfiledir = " my_bfile_dir" , bfilename = " my_bfile.txt" )
300
+ bfile, = cursor.fetchone()
301
+ print (bfile.read())
302
+
303
+ This will display::
304
+
305
+ This is my BFILE data
306
+
307
+ This fetched LOB can use :meth: `LOB.fileexists() ` to check if the file
308
+ referenced by the BFILE type LOB exists.
309
+
310
+ You can get the directory alias and file name of this fetched LOB by using
311
+ :meth: `LOB.getfilename() `. Also, you can set the directory alias and file name
312
+ for this fetched LOB by using :meth: `LOB.setfilename() `.
0 commit comments