1 #include
2 using namespacestd;3
4 #include
5 #include
6 #include
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 //include the OTL 4.0 header file
13 otl_connect db; //connect object
14
15 voidinsert()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,:f2),"
21 "(:f12,:f22),"
22 "(:f13,:f23),"
23 "(:f14,:f24),"
24 "(:f15,:f25)",25 //INSERT statement. Multiple sets of values can be used26 //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,:f2)", db //connect object
35 );36
37 char tmp[32];38 inti;39 for (i = 1; i <= 100; ++i) {40 sprintf(tmp, "Name%d", i);41 o << i <
49 void update(const intaf1)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"
55 "WHERE f1=:f1",56 //UPDATE statement
57 db //connect object
58 );59 o << "Name changed" <
61
62 }63
64 void select(const intaf1) {65 otl_stream i(50, //buffer size may be > 1
66 "select * from test_tab"
67 "where f1>=:f11"
68 "and f1<=:f12*2",69 //SELECT statement
70 db //connect object
71 );72 //create select stream
73
74 intf1;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 <
89 }90
91 intmain() {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 }