Use OTL for Database Programming

Source: Internet
Author: User
Tags db2 dsn

Operating Environment:
1. Operating System: Windows XP Professional with SP2.
2. Programming Environment: Visual C ++ 6.0 with SP6.
3. database environment: Access 2003.
OTL introduction:
OTL is the abbreviation of Oracle, ODBC and DB2-CLI Template Library, is a C ++ compiler to control the relational database template library, it currently supports almost all the current mainstream databases, such as Oracle, ms SQL Server, Sybase, Informix, MySQL, DB2, Interbase/Firebird, PostgreSQL, SQLite, SAP/DB, TimesTen, MS access and so on. In OTL, direct Oracle operations are performed through the OCI interface provided by Oracle, while operations on DB2 databases are performed through the CLI interface. For Ms databases and other databases, OTL only supports ODBC. Of course, Oracle and DB2 can also be indirectly manipulated by OTL using ODBC.
On MS Windows and UNIX platforms, OTL currently supports the following database versions: oracle 7 (directly using oci7) and Oracle 8 (directly using oci8 ), oracle 8i (using oci8i directly), Oracle 9i (using oci9i directly), Oracle 10g (using oci10g directly), DB2 (using DB2 CLI directly), ODBC 3.x, ODBC 2.5. The latest OTL version is 4.0. For more information, see http://otl.sourceforge.net/,http://otl.sourceforge.net/otlv4_h.zip.
Advantages:
A. Cross-Platform
B. High operation efficiency, equivalent to calling APIs directly in C Language
C. High development efficiency, at least easier to use than ado.net, and more concise
D. easy deployment, no ado components, No. NET Framework, etc.
Disadvantages:
A. The description documents and examples are not rich enough (temporary)
For more information, see http://otl.sourceforge.net/otl4_examples.zip.
Create a data source
1. choose Start> control panel, open the control panel page, double-click Administrative Tools, and then double-click data source (ODBC )", open "ODBC data source Manager" and select "system DSN ".
2. Click "add" to bring up the "Create a new data source" dialog box, and select "Microsoft Access Driver (*. mdb )".
3. click "finish". The "ODBC Microsoft Access installation" dialog box is displayed. Click "CREATE" to create a database. The "create database" dialog box is displayed. Add the database name my_db and select the database storage directory, click "OK", and then add the data source name my_db. Click "OK ".
4. Then there will be the data source we just added in the system data source.
5. Click OK to create the data source.
OTL Programming
An example is provided below:
1. Create a data table: testtable (columa int, Columb varchar (50), columc varchar (50 ))
2. Insert 100 data records. columa indicates the data ID range: 0-99, Columb = "test data % d", where % d = ID.
3. delete data smaller than 10 and greater than 90 in columa.
4. Update columc to Columb in the record whose columa is a multiple of 3.
The Code is as follows:

# Include <iostream>
Using namespace STD;
# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Define otl_odbc // compile OTL 4.0/ODBC
// # Define otl_odbc_unix // This macro is required if unixodbc is used in UNIX.
# Include "otlv4.h" // contains the OTL 4.0 header file
Otl_connect dB; // connection object

// This function inserts 100 data records. comula is the data ID and the value range is 0-99,
// Columb = "Test Data % d", where % d = ID
Void insert ()
// Insert rows into the table
{
// Open a common stream and insert multiple pieces of data into the table as a template
Otl_stream
O (1, // The buffer value of the stream must be set to 1
"Insert into testtable values (: F1 <int>,: F2 <char [50]>,: F3 <char [50]> )",
// SQL statement
DB // connection object
);
Char tmp1 [32];
Char tmp2 [30];

For (INT I = 0; I <100; ++ I ){
Sprintf (tmp1, "Test Data % d", I );
Sprintf (tmp2 ,"");
O <I <tmp1 <tmp2;
}

}
// This function deletes data smaller than 10 and greater than 90 in columa.
Void delete_rows ()
{
Long RPC = otl_cursor: direct_exec (dB, "delete from testtable where columa <10 or columa> 90 ");
// RPC is the return value of the effect. otl_cursor: direct_exec is the direct execution of SQL statements.
Cout <"rows deleted:" <RPC <Endl;
}

// This function completes updating columc to Columb in the record whose columa is a multiple of 3
Void Update ()
// Update the table
{
Otl_stream
O (1, // buffer Value
"Update testtable"
"Set columc =: F2 <char [50]>"
"Where columa =: F1 <int> ",
// Update statement
DB // connection object
);
Otl_stream C (1, "select Columb from testtable where columa =: F3 <int>", DB );
Char temp [10];
For (INT I = 10; I <91; I ++)
{
If (I % 3 = 0)
{
C <I;
C> temp;
O <temp <I;
}
}

}

Int main ()
{
Otl_connect: otl_initialize (); // initialize the ODBC Environment
Try {

DB. rlogon ("uid = Scott; Pwd = tiger; DSN = my_db"); // connect to ODBC
// Or use the following connection statement.
// DB. rlogon ("Scott/tiger @ Firebird"); // connect to ODBC, alternative format
// Of connect string

Otl_cursor: direct_exec
(
DB,
"Drop table testtable ",
Otl_exception: Disabled // disable OTL exceptions
); // Drop table

// Create a table here
Otl_cursor: direct_exec
(
DB,
"Create table testtable (columa int, Columb varchar (50), columc varchar (50 ))"
); // CREATE TABLE

Insert (); // insert records into the table
// Update (10); // update records in the table
Delete_rows ();
Update ();

}

Catch (otl_exception & P) {// intercept OTL exceptions
Cerr <p. MSG <Endl; // print out error message
Cerr <p. cmd_text <Endl; // print out SQL that caused the error
Cerr <p. sqlstate <Endl; // print out sqlstate message
Cerr <p. var_info <Endl; // print out the variable that caused the error
}

DB. logoff (); // disconnect from the database

Return 0;

}

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.