1、目录知识
(1)、基于服务端 vs 基于客户端
文件的路径都是相对于服务端目录的路径。
(2)、如何创建一个目录对象?
为了创建目录,必须具有DBA角色或者赋予了CREATE ANY DIRECTORY权限。
示例:
Window平台
CONNECT system/manager CREATE OR REPLACE DIRECTORY my_dir as ‘D:\DataPump‘; CREATE OR REPLACE DIRECTORY my_logdir as ‘E:\logs‘; GRANT read, write ON DIRECTORY my_dir TO scott; GRANT read, write ON DIRECTORY my_logdir TO scott;
Unix平台
CONNECT system/manager GRANT CREATE ANY DIRECTORY TO scott; CONNECT scott/tiger CREATE OR REPLACE DIRECTORY my_dir as ‘/usr/DataPump‘; CREATE OR REPLACE DIRECTORY my_logdir as ‘/usr/logs‘;
如果普通用户被赋予了CREATE ANY DIRECTORY权限,那么用户就自动具备目录的READ和WRITE权限。
注意:CREATE DIRECTORY语句不会创建磁盘的真实目录,如果目录是无效的,数据泵作业会报错:
ORA-39002: invalid operation ORA-39070: Unable to open the log file. ORA-29283: invalid file operation ORA-06512: at "SYS.UTL_FILE", line 475 ORA-29283: invalid file operation
(3)、如何查询可用的目录?
可以使用如下SQL查询具有READ和WRITE权限的目录:
SELECT directory_name, grantee, privilege FROM user_tab_privs t, all_directories d WHERE t.table_name(+)=d.directory_name ORDER BY 1,2,3;
2、建测试表
create table bfile_test ( code number(6), value bfile )
3、代码(由ocilib demo增加注释而来)
#include "ocilib.h" int main(void) { OCI_Connection *cn; OCI_Statement *st; OCI_Resultset *rs; OCI_File *file; char buffer[256]; int n; if (!OCI_Initialize(NULL, NULL, OCI_ENV_DEFAULT | OCI_ENV_CONTEXT)) { printf("Error OCI_Initialize\n"); return -1; } cn = OCI_ConnectionCreate("hisd", "ha2", "peidian", OCI_SESSION_DEFAULT); if (!cn) { printf("Error OCI_ConnectionCreate\n"); return -1; } st = OCI_StatementCreate(cn); if (!st) { printf("Error OCI_StatementCreate\n"); return -1; } file = OCI_FileCreate(cn, OCI_CFILE); if (!file) { printf("Error OCI_FileCreate\n"); return -1; } if (!OCI_FileSetName(file, "MYDIR", "bbbb.txt")) { printf("Error OCI_FileSetName\n"); return -1; } /* check if faile exists */ if (OCI_FileExists(file)) { printf("file size : %d\n", OCI_FileGetSize(file)); printf("file dir : %s\n", OCI_FileGetDirectory(file)); printf("file name : %s\n", OCI_FileGetName(file)); }else { printf("Error OCI_FileExists\n"); return -1; } /* bind for inserting into table */ if(!OCI_Prepare(st, "insert into BFILE_TEST(code, value) values (1, :bfile)")) { printf("Error OCI_Prepare\n"); return -1; } if(!OCI_BindFile(st, ":bfile", file)) { printf("Error OCI_BindFile\n"); return -1; } if(!OCI_Execute(st)) { printf("Error OCI_Execute\n"); return -1; } if(!OCI_Commit(cn)) { printf("Error OCI_Commit\n"); return -1; } /* free local file object */ OCI_FileFree(file); printf("insert OK\n"); #if 0 /* fetch bfile data from table */ OCI_ExecuteStmt(st, "select code, value from BFILE_TEST"); rs = OCI_GetResultset(st); while (OCI_FetchNext(rs)) { file = OCI_GetFile(rs, 2); OCI_FileOpen(file); printf("file size %d\n", OCI_FileGetSize(file)); printf("file dir %s\n", OCI_FileGetDirectory(file)); printf("file name %s\n", OCI_FileGetName(file)); while (n = OCI_FileRead(file, buffer, sizeof(buffer)-1)) { buffer[n] = 0; printf(buffer); } OCI_FileClose(file); } OCI_Cleanup(); #endif return EXIT_SUCCESS; }
时间: 2024-10-12 03:14:59