一、編寫代碼
注:以下代碼來自OTL示例,略有改動
1. #include
2. using namespace std;
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. 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,:f2), "
21. " (:f12,:f22), "
22. " (:f13,:f23), "
23. " (:f14,:f24), "
24. " (:f15,:f25) ",
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,:f2)", 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 <
42. }
43. for (i = 101; i <= 103; ++i) {
44. sprintf(tmp, "Name%d", i);
45. o2 <
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 "
55. " WHERE f1=:f1",
56. // UPDATE statement
57. db // connect object
58. );
59. o <
60. o <
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 "
68. " and f1<=:f12*2",
69. // SELECT statement
70. db // connect object
71. );
72. // create select stream
73.
74. int f1;
75. char f2[31];
76.
77. i <
78. while (!i.eof()) { // while not end-of-data
79. i >> f1;
80. cout <
81. i >> f2;
82. if (i.is_null())
83. cout <
84. else
85. cout <
86. cout <
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 <
114. cerr <
115. cerr <
116. cerr <
117. }
118.
119. db.logoff(); // disconnect from ODBC
120.
121. return 0;
122.
123. }
二、編譯代碼
g++-o"otl_test2" otl_test2.cpp -lmyodbc3
三、運行程序 otl_test
$ ./otl_test2
結果如下:
f1=8, f2=Name8
f1=9, f2=Name9
f1=10, f2=Name changed
f1=11, f2=NULL
f1=12, f2=Name12
f1=13, f2=Name13
f1=14, f2=Name14
f1=15, f2=Name15
f1=16, f2=Name16