otl连接mysql_使用OTL操作MySQL数据库

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 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值