OCI (Oracle call interface) Interface Programming (Oracle)

Source: Internet
Author: User

OCI is an application development tool developed by Oracle. It is a server that accesses the Oracle database to control the execution of various SQL statements, then create the application interface (API) of the application ). It supports all SQL data definitions, data operations, queries, transaction management, and other operations. It supports data types, calls, syntaxes, and semantics of C and C ++. It provides a set of interface subroutines (functions) that can access the Oracle database ).
The OCI program is essentially a program written in advanced languages. It features internal calls to the OCI sub-function library through third-generation programming languages (such as C language) to access the Oracle database.

Basic OCI programming process:

1. Create and initialize the OCI environment. The OCI environment is the operating environment of the OCI function. Before calling other functions, you must call the ociinitialize () and ocienvinit () functions to create and initialize the OCI environment, other OCI functions can only be executed in this environment.
Swordocienvinit (ocienv ** envhpp, ub4mode, size_txtramemsz, dvoid ** usrmempp );
Parameters:
Envhp: Environment handle pointer
Mode: initialization mode, oci_default, oci_threaded, etc.
Xtramemsz: size of memory to be allocated in the Application
Usrmempp: returns the pointer to the allocated user memory.
2. Apply for a handle
A handle is a pointer to the memory area allocated by the OCI database. The data in the memory area is maintained by the OCI database, and applications can access the data through the handle.
A handle can be used to store context or connection information (such as the Environment or service context handle). It is managed by the Link Library and can simplify programming. Ociatrrtget () and ociatrrset () can be used to access the OCI interfaces that obtain or set attributes to store information in the handle.
Generally, the following types of handles need to be applied:
Server handle ociserver, handle type oci_htype_server
Error handle ocierror, used to capture OCI error information, handle type oci_htype_error
Transaction handle ocisession, handle type oci_htype_session
Context handle ocisvcctx, handle type oci_htype_svcctx
SQL statement handle ocistmt, handle type oci_htype_stmt
Handle attributes to be set:
Server instance:
Handle type: oci_htype_svcctx; attribute type: oci_attr_server
User name for data connection:
Handle type: oci_htype_session; attribute type: oci_attr_username
User Password:
Handle type: oci_htype_session; attribute type: oci_attr_password
Transaction:
Handle type: oci_htype_svcctx; attribute type: oci_attr_session
Ocihandlealloc () -- allocate a handle and return the newly initialized handle pointer.
Swordocihandlealloc (constdvoid * parenth, dvoid ** hndlpp, ub4type, size_txtramem_sz, dvoid ** usrmempp );
Parameters:
Parenth: the parent handle of the new application handle. It is generally an OCI environment handle.
Hndlpp: New handle applied
Type: OCI handle type
Xtramem_sz: size of memory allocated to the handle
Usrmempp: memory block pointer applied
3. Connect to the server and establish a session
Ociserverattach (m_srvhp, m_errhp, (text *) "", strlen (""), oci_default); establish a connection with the specified server
Parameters:
Text *: The type variable is null, indicating to connect to the default database service.
Oci_defaul: indicates that the application's operation mode is blocking. In this mode, the control is returned to the client application only after the OCI call is complete.
Here, we will only introduce single-user and single-connection scenarios:
Ocilogon (envhp, errhp, & svchp, (text *) "HR", namelen, (text *) "HR", passwdlen, (text *) "oracledb", dbnamelen );
After this method is used, servicecontext, server, and andusersessionhandles are read-only and cannot be rewritten through ociattrset.
After you use ocilogon (), you must use ocilogoff () to terminate the program.
4. Execute SQL statements and process data.
5. End the session and disconnect the database.
Use ocisessionend () to delete each user session.
Ocilogoff (m_svchp, m_errhp );
6. disconnect from the data source and release the handle.
Ociserverdetach () is disconnected from the Data source:
Ociserverdetach (m_srvhp, m_errhp, oci_default );
Ocihandlefree () releases all allocated handles
Ocihandlefree-release handle
Swordocihandlefree (dvoid * hndlp, ub4type );
Parameters:
* Hndlp: handle to be released
Ub4type: handle type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.