C ++ class library: OTL connects to MySQLODBC database (insert, update, select) _ MySQL

Source: Internet
Author: User
Tags dsn sybase
I. INTRODUCTION OTL is a general-purpose database with pure C43; 43 connected to the template library. it supports various popular databases such as Oracle, Sybase, MySQL, PostgreSQL, EnterpriseDB, SQLite, and MSACCESS, firebird, etc. it is a cross-platform class library. I. INTRODUCTION

OTL is a pure C ++ universal database connection template library, can support a variety of popular databases, such as Oracle, Sybase, MySQL, PostgreSQL, EnterpriseDB, SQLite, ms access, Firebird and so on. it is a cross-platform class library that can be used in MS Windows, Linux/Unix/Mac OS X.

OTL is easy to use, as long as the header file contains: # include "otlv4.h". In fact, the entire OTL is A. H file, which is very convenient to use.

My download space:

Code: http://download.csdn.net/detail/u013354805/9057229

Document: http://download.csdn.net/detail/u013354805/9057243

Case: http://download.csdn.net/detail/u013354805/9057273

Official: http://otl.sourceforge.net/

II. usage:

1. first, specify the database type to be connected. OTL uses a macro definition to specify the database type to be connected. OTL initializes the database connection environment according to the macro definition.

Related macro definition list: http://otl.sourceforge.net/otl3_compile.htm

For example, # define OTL_ODBC_MYSQL indicates connecting to the ODBC MySQL database.

2. case:

1) specify the database type to connect:

# Define OTL_ODBC_MYSQL // specify the connected database type

2) import the OTL 4 header file

#include 
 
   // include the OTL 4 header file
 

3) define a database instance:

Otl_connect db; // defines the database instance

4) initialize the ODBC environment:

otl_connect::otl_initialize();

5) connect to ODBC:

db.rlogon("UID=scott;PWD=tiger;DSN=mysql"); // connect to ODBC

6). delete a table:

otl_cursor::direct_exec   (    db,    "drop table test_tab",    otl_exception::disabled // disable OTL exceptions   ); // drop table

7). create a table:

otl_cursor::direct_exec   (    db,    "create table test_tab(f1 int, f2 varchar(30))"    );  // create table

8) Insert statement:

// Insert rows into table void insert () {otl_stream o (1, // buffer size shocould be = 1 always on INSERT "insert into test_tab values (: f1
 
  
,: F2
  
   
) ", // SQL statement db // connect object); char tmp [32]; for (int I = 1; I <= 100; ++ I) {sprintf (tmp, "Name % d", I); o <
   
    
    


9). Update statement:

// Update row data into tablevoid update (const int af1) {otl_stream o (1, // buffer size shocould be = 1 always on UPDATE "UPDATE test_tab" "SET f2 =: f2
     
      
"" WHERE f1 =: f1
      
       
", // UPDATE statement db // connect object); o <" Name changed "<
       
10). Select statement:

// MyODBC does not allow any input bind variables in the WHERE clause // in a SELECT statement. // Therefore, the SELECT statement has to be generated literally. void select (const int af1) {char character BUF [1024]; sprintf (character BUF, "select * from test_tab where f1 >=% d and f1 <= % d * 2 ", af1, af1); otl_stream I (50, // buffer size may be> 1 bytes BUF, // SELECT statement db // connect object); // create select str Eam int f1; char f2 [31]; while (! I. eof () {// while not end-of-data I> f1; cout <"f1 =" <
        
         
11). Disconnect ODBC:

db.logoff(); // disconnect from ODBC

12). case:

Int main () {otl_connect: otl_initialize (); // initialize ODBC environment try {db. rlogon ("UID = scott; PWD = tiger; DSN = mysql"); // connect to ODBC // db. rlogon ("scott/tiger @ mysql"); // connect to ODBC, alternative format // of connect string otl_cursor: direct_exec (db, "drop table test_tab", otl_exception :: disabled // disable OTL exceptions); // drop table otl_cursor: direct_exec (db, "create table test_tab (f1 int, f2 varchar (30 ))"); // create table insert (); // insert records into the table update (10); // update records in the table select (8 ); // select records from the table} catch (otl_exception & p) {// intercept OTL exceptions cerr <
          
           
13). running result:

Outputf1=8, f2=Name8f1=9, f2=Name9f1=10, f2=Name changedf1=11, f2=NULLf1=12, f2=Name12f1=13, f2=Name13f1=14, f2=Name14f1=15, f2=Name15f1=16, f2=Name16

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.