C ++ Class Library: OTL connects to the MySQL ODBC database (insert, update, select) and otlodbc

Source: Internet
Author: User

C ++ Class Library: OTL connects to the MySQL ODBC database (insert, update, select) and otlodbc

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 <otlv4.h> // 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 should be == 1 always on INSERT              "insert into test_tab values(:f1<int>,:f2<char[31]>)",                  // SQL statement              db // connect object             ); char tmp[32]; for(int i=1;i<=100;++i) {  sprintf(tmp,"Name%d",i);  o<<i<<tmp; }}


9). Update statement:

// update row data into tablevoid update(const int af1){  otl_stream o(1, // buffer size should be == 1 always on UPDATE  "UPDATE test_tab "  "   SET f2=:f2<char[31]> "  " WHERE f1=:f1<int>",  // UPDATE statement  db // connect object ); o<<"Name changed"<<af1; o<<otl_null()<<af1+1; // set f2 to NULL}

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 stmbuf[1024]; sprintf(stmbuf,         "select * from test_tab where f1>=%d and f1<=%d*2",         af1,         af1        ); otl_stream i(50, // buffer size may be > 1              stmbuf, // SELECT statement              db // connect object             );    // create select stream  int f1; char f2[31]; while(!i.eof()) { // while not end-of-data  i>>f1;  cout<<"f1="<<f1<<", f2=";  i>>f2;  if(i.is_null())   cout<<"NULL";  else   cout<<f2;  cout<<endl; }}

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<<p.msg<<endl; // print out error message  cerr<<p.stm_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 ODBC return 0;}

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.

Related Article

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.