Use otl4 connect to MySQL example 83

Source: Internet
Author: User

# Include <iostream>
# Include <vector>
# Include <iterator>
# Include <string>
# Include <cstring>
# Include <cstdlib>

# Define otl_odbc_mysql // compile OTL 4/myodbc
// # Define otl_odbc_unix // uncomment this line if unixodbc is used
# Define otl_stl // turn on STL features
# Ifndef otl_ansi_cpp
# Define otl_ansi_cpp // turn on ansi c ++ typecasts
# Endif
# Include <otlv4.h> // include the OTL 4 header file

Using namespace STD;

Otl_connect dB; // connect object

// Row container class
Class row {
Public:
Int F1;
String F2;

// Default constructor
Row ():
F1 (0 ),
F2 ()
{
}

// Destructor
~ Row (){}

// Copy constructor
Row (const row & Arow ):
F1 (Arow. F1 ),
F2 (Arow. F2)
{
}
 
// Assignment operator
Row & operator = (const row & Arow)
{
This-> F1 = Arow. F1;
This-> F2 = Arow. F2;
Return * this;
}

};

// Redefined operator> for reading row & from otl_stream
Otl_stream & operator> (otl_stream & S, row & Row)
{
S> row. F1> row. F2;
Return S;
}

// Redefined operator <for writing row & into otl_stream
Otl_stream & operator <(otl_stream & S, const row & Row)
{
S <row. F1 <row. F2;
Return S;
}

// Redefined operator <writing row & into ostream
Ostream & operator <(ostream & S, const row & Row)
{
S <"f1 =" <row. F1 <", f2 =" <row. F2;
Return S;
}

Void insert ()
// Insert rows into table
{
Otl_stream O (1, // buffer size shocould be = 1 on insert
"Insert into test_tab values (: F1 <int>,: F2 <char [31]> )",
// Insert statement
DB // connect object
);

Row R; // single row Buffer
Vector <row> VO; // vector of rows

// Populate the Vector
For (INT I = 1; I <= 100; ++ I ){
R. F1 = I;
R. F2 = "namexxx ";
VO. push_back (R );
}

Cout <"VO. size =" <static_cast <int> (VO. Size () <Endl;

// Insert vector into table
Copy (VO. Begin (),
VO. End (),
Otl_output_iterator <row> (o)
);

}

Void select (const int AF1)
{

Char character Buf [1024];
# If defined (_ msc_ver)
# If (_ msc_ver> = 1400) // VC ++ 8.0 or higher
Sprintf_s (inclubuf, sizeof (inclubuf ),
"Select * From test_tab where F1 >=% D and F1 <= % d * 2 ",
AF1,
AF1
);
# Else
Sprintf (Alibaba Buf,
"Select * From test_tab where F1 >=% D and F1 <= % d * 2 ",
AF1,
AF1
);
# Endif
# Else
Sprintf (Alibaba Buf,
"Select * From test_tab where F1 >=% D and F1 <= % d * 2 ",
AF1,
AF1
);
# Endif

Otl_stream I (50, // buffer size may be> 1
Explain Buf, // SELECT statement
DB // connect object
);
// Create select stream
 
Vector <row> V; // vector of rows

// Copy all rows to be fetched into the vector

Copy (otl_input_iterator <row, ptrdiff_t> (I ),
Otl_input_iterator <row, ptrdiff_t> (),
Back_inserter (V ));

Cout <"size =" <static_cast <int> (V. Size () <Endl;

// Send the vector to cout
Copy (V. Begin (), V. End (), ostream_iterator <row> (cout, "\ n "));

}

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

DB. rlogon ("uid = Scott; Pwd = tiger; DSN = MySQL"); // connect to ODBC

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 table
Select (8); // select records from table

}

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. var_info <Endl; // print out the variable that caused the error
}

DB. logoff (); // disconnect from ODBC

Return 0;

}

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.