当前位置导航:炫浪网>>网络学院>>编程开发>>Oracle教程

基于Oracle OCI的数据访问C语言接口ORADBI

    ORADBI是我在Oracle OCI(Oracle 调用接口)基础上开发的,支持Oracle8i、9i、10g等数据库。根据Oracle公司的介绍:OCI是如此可靠,以至于Oracle数据库中的每一条SQL语句都通过OCI来执行。当应用程序开发人员需要面向Oracle数据库服务器的最强大的接口时,他们将调用 Oracle调用接口(OCI)。OCI提供了对所有Oracle数据库功能的最全面的访问。OCI API中包含了最新的性能、可伸缩性和安全性特性。

    采用OCI的最大好处就是:它是最切近Oracle底层的技术,因此,效率是最高的。同时,它是跨平台的。因此,在我给出的ORADBI库,除了OCI之外,没有其他外部依赖,稍加改动,就可以移植到非Windows平台上或其他嵌入式平台。

    然而,OCI也比较不容易使用。它的功能之强,粒度之细,语法之烦琐,都不适合今天熟悉了快速开发模式的人员使用。然而,OCI的高效,直接根植于数据库核心,跨平台的语言特性,是其他如OO4O、OLEDB、ADO等COM方式不具备的。我最初开发Oralce数据库是4年以前,使用的是OO4O.直到最近,使用OCI成了我的爱好。于是,写了一套OCI的Helper库,方便OCI的使用。我不敢说是OCI的Wrapper库,因为我没能力包装OCI的方方面面。然而,我的ORADBI已经足够我(可能也包括你们)日常使用了。我就是用这套ORADBI操纵BLOB等大数据字段的。

    公布这套ORADBI库,没有任何商业上的风险:我拥有这个库的全部版权,任何个人或团体可以为任何目的免费地、无限制地使用ORADBI库,前提是不能声明对ORADBI库的版权。对使用ORADBI库所造成的任何后果,我不负任何责任。如果这套软件对您的工作产生了很大的帮助,我不拒绝任何您的资助。

    好了,让我们概览一下这个库,它包含下列文件:

.h文件                 .c文件
oradbi.h*             oradbi.c
oracol.h              oracol.c
oraconn.h           oraconn.c
oradate.h            oradate.c
oraerr.h               oraerr.c
oraparam.h         oraparam.c

orarows.h            orarows.c
orastmt.h             orastmt.c
oratype.h

unistd.h
list.h                    list.c
hashmap.h        hashmap.c

    其中,oradbi.h是你唯一需要包含的头文件。当把ORADBI作为动态库连接到你的工程里使用时,你在你的项目里加入类似下面的代码:

// test.cpp
#include "../oradbi/oradbi.h"

#ifdef _DEBUG
  #pragma comment(lib, "../oradbi/debug/oradbid.lib")
#else
  #pragma comment(lib, "../oradbi/release/oradbi.lib")
#endif

    下面是oradbi.h的全部内容:

/*======================================================================
    oradbi.h - Created by cheungmine, All rights reserved.       
               First : Mar. 8, 2008
======================================================================*/
#ifndef ORADBI_H_INCLUDED
#define ORADBI_H_INCLUDED

#ifdef __cplusplus
extern "C" {
#endif

#ifdef ORADBI_DLLEXPORT
#  define ORADB_CALL __declspec(dllexport)
#  define ORADB_CALL1(x)  __declspec(dllexport) x
#endif

#ifndef ORADB_CALL
#  define ORADB_CALL
#endif

#ifndef ORADB_CALL1
#  define ORADB_CALL1(x)      x  ORADB_CALL
#endif

/* NOT SUPPORT UNICODE */
#ifdef UNICODE
    #pragma message( __FILE__" Warning UNICODE: Cannot use UNICODE" )
#endif

/* NOT SUPPORT UNICODE */
#ifdef _UNICODE
    #pragma message( __FILE__" Warning UNICODE: Cannot use UNICODE" )
#endif

#include "unistd.h"
#include "oratype.h"
#include "oraerr.h"

/*======================================================================
                            connection Functions - oraconn.c
======================================================================*/
/* connects the application to the oracle server. */
lresult ORADB_CALL    ORA_connection_create  (
                            OUT ora_connection    *connection,
                            IN  const char        *service_name,
                            IN  const char        *username,
                            IN  const char        *password,
                            IN  ulong            env_mode,            /* =OCI_THREADED|OCI_OBJECT */
                            IN  BOOL            non_blocking_mode,    /* =FALSE */
                            OUT ora_error_t        *error                /* NULL for no err msg return */
                            );

/* disconnects the application from the oracle server. */
void    ORADB_CALL    ORA_connection_free (
                            IN  ora_connection    connection
                            );

/* executes a sql statement with no result */
lresult ORADB_CALL    ORA_connection_execute (
                            IN  ora_connection    connection,
                            IN  const char        *sql_block,
                            IN  int                sql_len,        /* -1 for strlen called*/
                            OUT ora_error_t        *error            /* NULL for no err msg return */
                            );

/* prepares (and returns) a sql statement for execution */
lresult ORADB_CALL    ORA_connection_prepare (
                            IN  ora_connection    connection,
                            IN  const char        *sql_block,
                            IN  int             sql_len,        /* -1 for strlen called*/
                            OUT ora_sqlstmt        *sqlstmt,        /* a sql statement for execution returned */
                            OUT ora_error_t        *error            /* NULL for no err msg return */
                            );

/* executes a select sql statement and return the result set */
lresult ORADB_CALL    ORA_connection_select (
                            IN  ora_connection    connection,
                            IN  const char        *sql_select,
                            IN  int             sql_len,        /* -1 for strlen called*/
                            OUT ora_rowset      *rowset,        /* the result set returned */
                            OUT ora_error_t        *error            /* NULL for no err msg return */
                            );

/* commits changes */
lresult ORADB_CALL    ORA_connection_commit (
                            IN  ora_connection    connection,
                            OUT ora_error_t        *error            /* NULL for no err msg return */
                            );

/* rollbacks changes */
lresult ORADB_CALL    ORA_connection_rollback (
                            IN  ora_connection    connection,
                            OUT ora_error_t        *error            /* NULL for no err msg return */
                            );

/*======================================================================
                            sqlstmt Functions - orastmt.c
======================================================================*/
lresult ORADB_CALL    ORA_sqlstmt_create  (
                            OUT ora_sqlstmt        *sqlstmt,
                            IN  ora_connection    connection,                               
                            IN  const char        *sql_block,
                            IN    int                sql_len,          /* -1 for strlen to be called*/
                            IN  ushort            fetch_size,         /* 0 for default or bind array size for INSERT */           
                            OUT ora_error_t        *error             /* NULL for no err msg return */
                            );

void    ORADB_CALL    ORA_sqlstmt_free  (
                            IN  ora_sqlstmt        sqlstmt
                            );

/* executes a sql statement with no output parameters */
lresult    ORADB_CALL    ORA_sqlstmt_execute  (
                            IN  ora_sqlstmt        sqlstmt,
                            IN  ORADBI_ExecMode mode,            /* execute mode: MOD_DEFAULT=0 */
                            IN  ushort            iters,            /* 0 is default */
                            OUT ora_error_t        *error            /* NULL for no err msg return */
                            );

/* executes a select sql statement and returns the result set */
lresult    ORADB_CALL    ORA_sqlstmt_select  (
                            IN  ora_sqlstmt        sqlstmt,
                            IN  ORADBI_ExecMode mode,            /* execute mode: MOD_DEFAULT=0 */
                            OUT ora_rowset        *rowset,
                            OUT ora_error_t        *error            /* NULL for no err msg return */
                            );

/* binds a named variable to sqlstmt but for lob, when type is set to ODT_UNKNOWN type is taken from name's prefix */
lresult    ORADB_CALL    ORA_sqlstmt_bind  (
                            IN  ora_sqlstmt        sqlstmt,
                            IN  const char        *name,        /* param name bound */
                            IN  ORADBI_DataType type,        /* param type bound. set ODT_UNKNOWN for simplely. cannot be a lob type */
                            IN  int                size,        /* param size for TEXT, LOB. 0 for others */
                            OUT ora_param        *param,        /* a out param bound to sqlstmt */
                            OUT ora_error_t        *error        /* NULL for no err msg return */
                            );

/* binds a named variable to sqlstmt only with lob type */
lresult    ORADB_CALL    ORA_sqlstmt_bind_lob  (
                            IN  ora_sqlstmt        sqlstmt,
                            IN  const char        *name,        /* param name bound */
                            IN  int              max_size,   /* maximum size of lob data, 0 for default. if 0, client_data must be a valid pointer */
                            IN  ORADBI_InBind    *inbind,    /* can be null. if null, max_size must be a  meaningful value not more than 65535 */
                            IN  ORADBI_OutBind    *outbind,   /* can be null */                           
                            OUT ora_param        *param,        /* a out param bound to sqlstmt */
                            OUT ora_error_t        *error        /* NULL for no err msg return */
                            );

/* accesses param in the sqlstmt by name. gets a reference to param */
ora_param    ORADB_CALL    ORA_sqlstmt_get_param_by_name  (
                            IN  ora_sqlstmt        sqlstmt,
                            IN  const char*     param_name,    /* MUST be a valid name string */
                            OUT ora_error_t        *error        /* NULL for no err msg return */
                            );

/* accesses param in the sqlstmt by index: 1-based. gets a reference to param */
ora_param    ORADB_CALL    ORA_sqlstmt_get_param_by_index  (
                            IN  ora_sqlstmt        sqlstmt,
                            IN  ushort          param_index,    /* PARAM_INDEX_BASE based */            
                            OUT ora_error_t        *error            /* NULL for no err msg return */
                            );


/*======================================================================
                            rowset Functions - orarows.c
======================================================================*/
/* returns number of rows fetched so far */
lresult    ORADB_CALL  ORA_rowset_num_rows (
                            IN  ora_rowset        rowset,
                            OUT size_t            *num_rows,
                            OUT ora_error_t        *error            /* NULL for no err msg return */
                            );

/* returns number of columns */
lresult ORADB_CALL  ORA_rowset_num_columns (
                            IN  ora_rowset        rowset,
                            OUT size_t            *num_cols,
                            OUT ora_error_t        *error        /* NULL for no err msg return */
                            );

/* fetches next row of record. TRUE for NOT completed; FALSE for completed */
BOOL ORADB_CALL  ORA_rowset_next (
                            IN  ora_rowset        rowset
                            );

/* frees a ora_rowset. a freed rowset handle is an invalid pointer and cannot be used any more */
void    ORADB_CALL    ORA_rowset_free  (
                            IN  ora_rowset        rowset
                            );

/* accesses column in the current row by name. gets a reference to column */
ora_column    ORADB_CALL    ORA_rowset_get_column_by_name  (
                            IN  ora_rowset        rowset,
                            IN  const char*     col_name,    /* MUST be a valid name string */
                            OUT ora_error_t        *error        /* NULL for no err msg return */
                            );

/* accesses column in the current row by index: 1-based. gets a reference to column */
ora_column    ORADB_CALL    ORA_rowset_get_column_by_index  (
                            IN  ora_rowset        rowset,
                            IN  ushort          col_index,    /* COLUMN_INDEX_BASE based */            
                            OUT ora_error_t        *error        /* NULL for no err msg return */
                            );

共2页 首页 上一页 1 2 下一页 尾页 跳转到
相关内容
赞助商链接