C + + Class Library: OTL connect MySQL ODBC database (INSERT, UPDATE, select)

Source: Internet
Author: User
Tags dsn odbc sprintf

I. INTRODUCTION

OTL is a pure C + + Generic database connection template library that 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 simple to use, as long as the header file contains: #include "otlv4.h" can, in fact, the entire OTL is a. h file, which is extremely 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/

Two. How to use:

1. First specify the type of database to connect to, and OTL use the macro definition to specify the type of database to connect to. OTL initializes the context of the database connection based on this macro definition.

List of related macro definitions: http://otl.sourceforge.net/otl3_compile.htm

For example: #define otl_odbc_mysql means connecting to the ODBC MYSQL database.

2, Case:

1) Specify the database type of the connection:

<span style= "FONT-SIZE:14PX;" > #define OTL_ODBC_MYSQL//Specify the type of database for the connection </span>

2) Import OTL 4 header file

<span style= "FONT-SIZE:14PX;" > #include <otlv4.h>//include the OTL 4 header file</span>


3) Define the DB instance:

<span style= "FONT-SIZE:14PX;" >otl_connect DB; Define a DB instance </span>

4) Initialize the ODBC environment:

<span style= "FONT-SIZE:14PX;" >otl_connect::otl_initialize ();</span>

5) Connect to ODBC:

<span style= "FONT-SIZE:14PX;" >db.rlogon ("Uid=scott; Pwd=tiger;dsn=mysql "); Connect to Odbc</span>

6). Delete the table:

<span style= "FONT-SIZE:14PX;" >  otl_cursor::d irect_exec   (    db,    "drop table Test_tab",    otl_exception::d isabled//disable OTL exceptions   ); Drop table</span>

7). Create a table:

<span style= "FONT-SIZE:14PX;" >  otl_cursor::d irect_exec   (    db,    "CREATE TABLE Test_tab (f1 int, f2 varchar ())"    ;  Create table</span>

8). Insert Statement:

<span style= "FONT-SIZE:14PX;" >//Insert rows into table void Insert () {  otl_stream O (1,//buffer size should is = = 1 always on insert              "Inse RT 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; }}</span>


9). Update statement:

<span style= "FONT-SIZE:14PX;" >//update row data into Tablevoid update (const int AF1) {  otl_stream O (1,//buffer size should bes = = 1 always on U PDATE  "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}</span>

). Select statement:

<span style= "FONT-SIZE:14PX;"  >//MyODBC does not allow any input bind variables in the WHERE clause//in a select statement.//Therefore, the Select Statement have to is 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 (n,//buffer size may > 1              STMBUF,//SELECT statement              DB//Connect object             );    Create select stream  int F1; char f2[31]; while (!i.eof ()) {//When not end-of-data  i>>f1;  cout<< "f1=" <<f1<< ", f2=";  i>>f2;  if (I.is_null ())   cout<< "null";  else   cout<<f2;  cout<<endl; }}</span>

11). Disconnect ODBC:

<span style= "FONT-SIZE:14PX;" > Db.logoff (); Disconnect from odbc</span>

12). Case:

<span style= "FONT-SIZE:14PX;" >int Main () {otl_connect::otl_initialize ();//Initialize ODBC environment try {db.rlogon ("Uid=scott; Pwd=tiger;dsn=mysql "); Connect to ODBC//Db.rlogon ("scott/[email protected]");   Connect to ODBC, alternative format//of connect string otl_cursor::d irect_exec (DB, "drop table Test_tab", otl_exception::d isabled//disable OTL exceptions);  drop table Otl_cursor::d irect_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<<end L 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;} </span>

13). Operation Result:

<span style= "FONT-SIZE:14PX;" >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</span>

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + Class Library: OTL connect MySQL ODBC database (INSERT, UPDATE, select)

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.