首先重点推荐介绍otl介绍及用法的文章:http://www.cnblogs.com/fnlingnzb-learner/p/5835560.html

一、编写代码
注:以下代码来自OTL示例,略有改动
#include
using namespace std;#include
#include
#include #define OTL_ODBC // Compile OTL 4.0/ODBC
// The following #define is required with MyODBC 3.51.11 and higher
#define OTL_ODBC_SELECT_STM_EXECUTE_BEFORE_DESCRIBE
#define OTL_ODBC_UNIX // uncomment this line if UnixODBC is used
#include  // include the OTL 4.0 header file
otl_connect db; // connect objectvoid insert()
// insert rows into table
{otl_stream o(1, // buffer size should be == 1 always on INSERT"insert into test_tab values "" (:f1,:f2), "" (:f12,:f22), "" (:f13,:f23), "" (:f14,:f24), "" (:f15,:f25) ",// INSERT statement. Multiple sets of values can be used// to work around the lack of the bulk interfacedb // connect object
            );// If the number of rows to be inserted is not known in advance,// another stream with the same INSERT can be openedotl_stream o2(1, // buffer size should be == 1 always on INSERT"insert into test_tab values "" (:f1,:f2)", db // connect object
            );char tmp[32];int i;for (i = 1; i <= 100; ++i) {sprintf(tmp, "Name%d", i);o << i << tmp;}for (i = 101; i <= 103; ++i) {sprintf(tmp, "Name%d", i);o2 << i << tmp;}
}void update(const int af1)
// insert rows into table
{otl_stream o(1, // buffer size should be == 1 always on UPDATE"UPDATE test_tab ""   SET f2=:f2 "" WHERE f1=:f1",// UPDATE statementdb // connect object
            );o << "Name changed" << af1;o << otl_null() << af1 + 1; // set f2 to NULL

}void select(const int af1) {otl_stream i(50, // buffer size may be > 1"select * from test_tab ""where f1>=:f11 ""  and f1<=:f12*2",// SELECT statementdb // connect object
            );// create select streamint f1;char f2[31];i << af1 << af1; // :f11 = af1, :f12 = af1while (!i.eof()) { // while not end-of-datai >> f1;cout << "f1=" << f1 << ", f2=";i >> f2;if (i.is_null())cout << "NULL";elsecout << f2;cout << endl;}}int main() {otl_connect::otl_initialize(); // initialize ODBC environmenttry {// db.rlogon("UID=xuanyuan;PWD=xuanyuan;DSN=examples"); // connect to ODBC        //其中dsn是odbc连接的名字,不是数据库的名字,otl是通过odbc的名字找到数据库的,          //而这个名字对于的配置里面,已经包含了IP,端口等信息,只要你提供用户名和密码就可以访问了          //见http://jingyan.baidu.com/article/8065f87f38b31423312498e4.html           db.rlogon("xuanyuan/xuanyuan@examples"); // connect to ODBC, alternative format of connect string
otl_cursor::direct_exec(db, "use examples");  // 此行在原示例代码中没有,必须使用use xxx切换数据库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)) type=innoDB"); // create table
insert(); // insert records into the tableupdate(10); // update records in the tableselect(8); // select records from the table
}catch (otl_exception& p) { // intercept OTL exceptionscerr << p.msg << endl; // print out error messagecerr << p.stm_text << endl; // print out SQL that caused the errorcerr << p.sqlstate << endl; // print out SQLSTATE messagecerr << p.var_info << endl; // print out the variable that caused the error
    }db.logoff(); // disconnect from ODBCreturn 0;}
二、编译代码
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

转载于:https://www.cnblogs.com/fnlingnzb-learner/p/5881927.html

Linux下使用OTL操作mysql数据库相关推荐

  1. linux下使用的mysql数据库,Linux下安装以及使用MySQL数据库

    1.官网下载mysql数据库:https://dev.mysql.com/downloads/mysql/ 2.linux 下可直接下载:wget https://cdn.mysql.com//Dow ...

  2. linux python开发环境sql数据迁移到mysql_linux环境下python怎样操作mysql数据库

    展开全部 linux环境下python怎样操作mysql数据库呢?方法如下: 首先在Linux环境下安装mysql-python 1.下载mysql-python 打开终端: cd /usr/loca ...

  3. linux下使用python操作mysql

    linux下使用python操作mysql 文章目录 linux下使用python操作mysql 一.Python 中MySQL的几个模块对象 1.模块 2.Connection 对象 3.Curso ...

  4. linux下使用C++操作mysql

    linux下使用C++操作mysql 文章目录 linux下使用C++操作mysql 一.常用类型介绍 1. MYSQL 2. MYSQL_ROW 3. MYSQL_FIELD 4. MYSQL_RE ...

  5. linux下c语言调用mysql,Linux下C语言操作MYSQL总结

    Linux下C语言操作MYSQL总结 发布时间:2006-12-17 00:40:00来源:红联作者:anciens 下文的用到的数据库表: 数据库中存在表table, 有name(varchar类型 ...

  6. Linux下使用C++操作redis数据库

    Linux下使用C++操作redis数据库 文章目录 Linux下使用C++操作redis数据库 一.安装配置hiredis.h 二.接口介绍 1.**`建立链接:redisConnect`** 2. ...

  7. linux otl 连接mysql_Linux下用OTL操作MySql(包括自己封装的类库及示例代码下载)...

    (1)首先安装MySql数据库服务: (2)安装navicat数据库客户端: 安装步骤:傻瓜安装,选择字符集的时候自己根据情况选择gbk2312,或者utf-8(一般涉及到网络传输或者跨平台,比如和j ...

  8. linux下的shell操作mysql

    (1)MySQL的启动 重启了一次服务器后,使用> mysql -u root -p登陆是出现下面的错误: ERROR 2002 (HY000): Can't connect to local ...

  9. Linux下的C#连接Mysql数据库

    今天在尝试在 Linux 系统下使用C#连接数据库,发现网上这方面的信息很少,所以就写一篇博客记录一下. Linux下这里使用的是mono. 首先是缺少Mysql.Data.dll这个库的,所以需要安 ...

最新文章

  1. 在 Linux“.NET研究” 操作系统中运行 ASP.NET 4 (下)
  2. lwip路由实现_基于LWIP协议栈对路由缓存数据结构实现改进设计
  3. 如何解决SSL/TLS握手过程中失败的错误?
  4. 基于ubuntu13.04搜狗输入法安装方法
  5. python爬虫进程和线程_python爬虫番外篇(一)进程,线程的初步了解-阿里云开发者社区...
  6. Flink 流批一体的实践与探索
  7. 高等组合学笔记(九): 球盒模型的十二模式,分拆的生成函数
  8. python天天向上的力量三天打鱼两天晒网_天天向上的力量
  9. 剑指 Offer II 061. 和最小的 k 个数对
  10. docker MySQL-错误:2059-Authentication plugin ‘caching_sha2_password‘ cannot be loaded
  11. 嵌入式开发的基础知识大揭秘
  12. android 5.1一键root工具箱,最新的安卓5.1.1 ROOT教程(不需要刷第三方内核)
  13. 鸟瞰Atlassian认证
  14. OllyDbg插件编写
  15. 字符数组与字符串的转换
  16. 软件工程作业之CSDN测评
  17. WebWork深入探索之初见端倪
  18. 不要再说微服务可以解决一切问题了
  19. VSFTPD服务器配置
  20. 【linux系列】内核编译最新教程+常见问题总结

热门文章

  1. 【hibernate系列】采用p6spy+SQLProfiler完整显示hibernate的S...
  2. 右键 Dos在这里 删除
  3. 2800:垂直直方图
  4. Python学习笔记——文件写入和读取
  5. SystemProperities
  6. Android开发之使用SharedPreferences实现QQ登陆的选项框记忆功能(源代码分享)
  7. 算法--旋转链表(Java)
  8. Ubuntu 调试的时候,不能查看变量值
  9. 如果看懂git -help
  10. 如何查看方法在哪里被调用