使用OTL操作MySQL数据库
1 #include <iostream> 2 using namespace std; 3 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 8 #define OTL_ODBC // Compile OTL 4.0/ODBC 9 // The following #define is required with MyODBC 3.51.11 and higher 10 #define OTL_ODBC_SELECT_STM_EXECUTE_BEFORE_DESCRIBE 11 #define OTL_ODBC_UNIX // uncomment this line if UnixODBC is used 12 #include <otlv4.h> // include the OTL 4.0 header file 13 otl_connect db; // connect object 14 15 void insert() 16 // insert rows into table 17 { 18 otl_stream o(1, // buffer size should be == 1 always on INSERT 19 "insert into test_tab values " 20 " (:f1<int>,:f2<char[31]>), " 21 " (:f12<int>,:f22<char[31]>), " 22 " (:f13<int>,:f23<char[31]>), " 23 " (:f14<int>,:f24<char[31]>), " 24 " (:f15<int>,:f25<char[31]>) ", 25 // INSERT statement. Multiple sets of values can be used 26 // to work around the lack of the bulk interface 27 db // connect object 28 ); 29 30 // If the number of rows to be inserted is not known in advance, 31 // another stream with the same INSERT can be opened 32 otl_stream o2(1, // buffer size should be == 1 always on INSERT 33 "insert into test_tab values " 34 " (:f1<int>,:f2<char[31]>)", db // connect object 35 ); 36 37 char tmp[32]; 38 int i; 39 for (i = 1; i <= 100; ++i) { 40 sprintf(tmp, "Name%d", i); 41 o << i << tmp; 42 } 43 for (i = 101; i <= 103; ++i) { 44 sprintf(tmp, "Name%d", i); 45 o2 << i << tmp; 46 } 47 } 48 49 void update(const int af1) 50 // insert rows into table 51 { 52 otl_stream o(1, // buffer size should be == 1 always on UPDATE 53 "UPDATE test_tab " 54 " SET f2=:f2<char[31]> " 55 " WHERE f1=:f1<int>", 56 // UPDATE statement 57 db // connect object 58 ); 59 o << "Name changed" << af1; 60 o << otl_null() << af1 + 1; // set f2 to NULL 61 62 } 63 64 void select(const int af1) { 65 otl_stream i(50, // buffer size may be > 1 66 "select * from test_tab " 67 "where f1>=:f11<int> " 68 " and f1<=:f12<int>*2", 69 // SELECT statement 70 db // connect object 71 ); 72 // create select stream 73 74 int f1; 75 char f2[31]; 76 77 i << af1 << af1; // :f11 = af1, :f12 = af1 78 while (!i.eof()) { // while not end-of-data 79 i >> f1; 80 cout << "f1=" << f1 << ", f2="; 81 i >> f2; 82 if (i.is_null()) 83 cout << "NULL"; 84 else 85 cout << f2; 86 cout << endl; 87 } 88 89 } 90 91 int main() { 92 otl_connect::otl_initialize(); // initialize ODBC environment 93 try { 94 95 // db.rlogon("UID=xuanyuan;PWD=xuanyuan;DSN=examples"); // connect to ODBC 96 db.rlogon("xuanyuan/xuanyuan@examples"); // connect to ODBC, alternative format of connect string 97 98 otl_cursor::direct_exec(db, "use examples"); // 此行在原示例代码中没有,必须使用use xxx切换数据库 99 otl_cursor::direct_exec(db, "drop table test_tab", 100 otl_exception::disabled // disable OTL exceptions 101 ); // drop table 102 103 otl_cursor::direct_exec(db, 104 "create table test_tab(f1 int, f2 varchar(30)) type=innoDB"); // create table 105 106 insert(); // insert records into the table 107 update(10); // update records in the table 108 select(8); // select records from the table 109 110 } 111 112 catch (otl_exception& p) { // intercept OTL exceptions 113 cerr << p.msg << endl; // print out error message 114 cerr << p.stm_text << endl; // print out SQL that caused the error 115 cerr << p.sqlstate << endl; // print out SQLSTATE message 116 cerr << p.var_info << endl; // print out the variable that caused the error 117 } 118 119 db.logoff(); // disconnect from ODBC 120 121 return 0; 122 123 }
请访问OTL的主页:http://otl.sourceforge.net/
文章转载自http://www.xuanyuan-soft.cn/lab/examples/122-cpp-otl-mysql

浙公网安备 33010602011771号