Home Database Mysql Tutorial C++用OTL访问Oracle数据库的例子

C++用OTL访问Oracle数据库的例子

Jun 07, 2016 pm 03:18 PM
c++ oracle example database access

在Windows平台,数据库的访问手段比较丰富,如ADO或者ODBC等,然而在UNIX/Linux平台上访问数据就不是那么容易了。 如果我们使用Java作为开发语言,那么JDBC可以提供数据访问的途径,但是如果用C/C++就没这么简单了,你必须使用最原始的C/C++调用接口来访问数

     在Windows平台,数据库的访问手段比较丰富,如ADO或者ODBC等,然而在UNIX/Linux平台上访问数据就不是那么容易了。 如果我们使用Java作为开发语言,那么JDBC可以提供数据访问的途径,但是如果用C/C++就没这么简单了,你必须使用最原始的C/C++调用接口来访问数据库。
       目前大型的数据库,如Oracel或者DB2都提供了C/C++的调用接口,但是作为开发人员使用这些接口是一件很头痛的事情,你必须要熟记每一个API,然而OTL给我带来了一个新的数据库访问方式。
      本文将通过一个例子来展示OTL是如何使用的。希望借此使得那些和我一样的OTL入门者少走一些弯路。

开发环境:Windows 2003 + Oracle 9i
开发工具:Dev C++  V 4.9.9.0

Oracle安装路径:C:/Oracle/

我们仅仅展示数据库的连接和释放。

 

首先,我们建立一个Dev C++项目,名为“OTL”

C++用OTL访问Oracle数据库的例子

在弹出的对话框中选择“Empty Project”
C++用OTL访问Oracle数据库的例子

接下来选择一个位置来保存工程文件
C++用OTL访问Oracle数据库的例子

 

之后,我们将向工程中添加我们的源代码文件,源代码文件如下:

#include
using namespace std;
#include

#define OTL_ORA9I // Compile OTL 4.0/OCI9i
#define OTL_ORA_TIMESTAMP // enable Oracle 9i TIMESTAMPs [with [local] time zone]
#include "otlv4.h" // include the OTL 4.0 header file

otl_connect db; // connect object


int
main()
{
    otl_connect::otl_initialize(); // initialize OCI environment 
    try
    {
        db.rlogon("scott/tiger@ORACLE9I"); // connect to Oracle 
        cout"Connect to Database"    }
    catch(otl_exception& p)
    { 
        // intercept OTL exceptions 
        cerr// print out error message 
        cerr// print out SQL that caused the error 
        cerr// print out SQLSTATE message 
        cerr// print out the variable that caused the error 
    }
 

    db.logoff(); // disconnect from Oracle

    return 0;
}

我们还需要将otlv4.h这个头文件添加到我们的工程中。

此时,我们需要设置一下头文件的路径和库文件的路径
C++用OTL访问Oracle数据库的例子

C++用OTL访问Oracle数据库的例子

此时,我们就可以编译并执行该程序了!

作者:http://allanyan.cnblogs.com/articles/105159.html

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement the Strategy Design Pattern in C++? How to implement the Strategy Design Pattern in C++? Jun 06, 2024 pm 04:16 PM

The steps to implement the strategy pattern in C++ are as follows: define the strategy interface and declare the methods that need to be executed. Create specific strategy classes, implement the interface respectively and provide different algorithms. Use a context class to hold a reference to a concrete strategy class and perform operations through it.

Similarities and Differences between Golang and C++ Similarities and Differences between Golang and C++ Jun 05, 2024 pm 06:12 PM

Golang and C++ are garbage collected and manual memory management programming languages ​​respectively, with different syntax and type systems. Golang implements concurrent programming through Goroutine, and C++ implements it through threads. Golang memory management is simple, and C++ has stronger performance. In practical cases, Golang code is simpler and C++ has obvious performance advantages.

What are the underlying implementation principles of C++ smart pointers? What are the underlying implementation principles of C++ smart pointers? Jun 05, 2024 pm 01:17 PM

C++ smart pointers implement automatic memory management through pointer counting, destructors, and virtual function tables. The pointer count keeps track of the number of references, and when the number of references drops to 0, the destructor releases the original pointer. Virtual function tables enable polymorphism, allowing specific behaviors to be implemented for different types of smart pointers.

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How to implement nested exception handling in C++? How to implement nested exception handling in C++? Jun 05, 2024 pm 09:15 PM

Nested exception handling is implemented in C++ through nested try-catch blocks, allowing new exceptions to be raised within the exception handler. The nested try-catch steps are as follows: 1. The outer try-catch block handles all exceptions, including those thrown by the inner exception handler. 2. The inner try-catch block handles specific types of exceptions, and if an out-of-scope exception occurs, control is given to the external exception handler.

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

How does Go WebSocket integrate with databases? How does Go WebSocket integrate with databases? Jun 05, 2024 pm 03:18 PM

How to integrate GoWebSocket with a database: Set up a database connection: Use the database/sql package to connect to the database. Store WebSocket messages to the database: Use the INSERT statement to insert the message into the database. Retrieve WebSocket messages from the database: Use the SELECT statement to retrieve messages from the database.

How to copy files using C++? How to copy files using C++? Jun 05, 2024 pm 02:44 PM

How to copy files in C++? Use std::ifstream and std::ofstream streams to read the source file, write to the destination file, and close the stream. 1. Create new streams of source and target files. 2. Check whether the stream is opened successfully. 3. Copy the file data block by block and close the stream to release resources.

See all articles