首先重点推荐介绍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 object void insert()
// insert rows into table
{
otl_stream o(, // 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 interface
db // connect object
); // If the number of rows to be inserted is not known in advance,
// another stream with the same INSERT can be opened
otl_stream o2(, // buffer size should be == 1 always on INSERT
"insert into test_tab values "
" (:f1,:f2)", db // connect object
); char tmp[];
int i;
for (i = ; i <= ; ++i) {
sprintf(tmp, "Name%d", i);
o << i << tmp;
}
for (i = ; i <= ; ++i) {
sprintf(tmp, "Name%d", i);
o2 << i << tmp;
}
} void update(const int af1)
// insert rows into table
{
otl_stream o(, // buffer size should be == 1 always on UPDATE
"UPDATE test_tab "
" SET f2=:f2 "
" WHERE f1=:f1",
// UPDATE statement
db // connect object
);
o << "Name changed" << af1;
o << otl_null() << af1 + ; // set f2 to NULL } void select(const int af1) {
otl_stream i(, // buffer size may be > 1
"select * from test_tab "
"where f1>=:f11 "
" and f1<=:f12*2",
// SELECT statement
db // connect object
);
// create select stream int f1;
char f2[]; i << af1 << af1; // :f11 = af1, :f12 = af1
while (!i.eof()) { // while not end-of-data
i >> f1;
cout << "f1=" << f1 << ", f2=";
i >> f2;
if (i.is_null())
cout << "NULL";
else
cout << f2;
cout << endl;
} } int main() {
otl_connect::otl_initialize(); // initialize ODBC environment
try { // 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 table
update(); // update records in the table
select(); // select records from the table } catch (otl_exception& p) { // intercept OTL exceptions
cerr << p.msg << endl; // print out error message
cerr << p.stm_text << endl; // print out SQL that caused the error
cerr << p.sqlstate << endl; // print out SQLSTATE message
cerr << p.var_info << endl; // print out the variable that caused the error
} db.logoff(); // disconnect from ODBC return ; }
二、编译代码
g++ -o"otl_test2" otl_test2.cpp -lmyodbc3
三、运行程序 otl_test
$ ./otl_test2
结果如下:
f1=, f2=Name8
f1=, f2=Name9
f1=, f2=Name changed
f1=, f2=NULL
f1=, f2=Name12
f1=, f2=Name13
f1=, f2=Name14
f1=, f2=Name15
f1=, f2=Name16

Linux下使用OTL操作mysql数据库的更多相关文章

  1. Linux下用OTL操作MySql(包含自己封装的类库及演示样例代码下载)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/ClamReason/article/details/23971805 首先重点推荐介绍otl介绍及使 ...

  2. Linux下使用Python操作MySQL数据库

    安装mysql-python 1.下载mysql-python 打开终端: cd /usr/local sudo wget http://nchc.dl.sourceforge.net/sourcef ...

  3. Linux下C语言操作MySQL数据库

    MySQL是Linux系统下广泛使用的开源免费数据库,是Linux应用程序数据存储的首选. Ubuntu下安装 […]

  4. 在linux下安装并配置mysql数据库

    在linux下安装并配置mysql数据库 工具/原料   MySql5.6 CentOS 方法/步骤   1 查找以前是否安装有mysql,使用下面命令: rpm -qa|grep -i mysql ...

  5. linux下应用crontab对mysql数据库进行定时备份

    linux下应用crontab对mysql数据库进行定时备份 @(编程) mysql数据库提供了备份命令mysqldump,可以结合crontab命令进行定时备份. 我写了一个mysqlbackup. ...

  6. linux下的shell操作mysql

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

  7. Linux下安装以及使用MySQL数据库

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

  8. linux下卸载和安装mysql数据库的方法

    1.1  MySQL下载 下载地址:http://www.mysql.com/downloads/mysql/5.5.html#downloads 版本:5.1.68 平台:linux general ...

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

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

随机推荐

  1. es高级部分

    1 关于机器 配置. 内存:上亿的数据一般需要64G内存的服务器.劲量不要使用小于32G 内存的服务器. cpu:es 对cpu 要求依赖不如内存.一般要求2-8 核就可以了. 磁盘:es 对磁盘依赖 ...

  2. thinkphp5 列表页数据分页查询-带搜索条件

    一.控制器部分 <?php namespace app\user\controller; use app\user\model\HelpCenterManual as HelpCenterMan ...

  3. str.replace()和re.sub()/calendar.month_abbr/re.subn()/upper和lower和capitalize/贪婪匹配和费贪婪匹配/re.S和re.DOTALL 笔记

    str.replace()可以进行简单的替换 >>> a = 'one.txt, index.py, index.php, index.html, index.js' >> ...

  4. 升级ambari、HDP版本(ambari 2.1升级到2.4、HDP2.3升级到2.5)

    转载自:http://blog.csdn.net/levy_cui/article/details/52461377 官方升级版本说明 http://docs.hortonworks.com/HDPD ...

  5. Pandas的使用(2)

    Pandas的使用(2) 1.新建一个空的DataFrame数据类型 total_price = pd.DataFrame() #新建一个空的DataFrame 2.向空的DataFrame中逐行添加 ...

  6. golang 指针在struct里的应用

    type aa struct { b *int c string } func main() { var data int = 0 var ip *int /* 声明指针变量 */ ip = & ...

  7. springboot下多线程开发注意事项

    基于springboot的多线程程序开发过程中,由于本身也需要注入spring容器进行管理,才能发挥springboot的优势.所以这篇文字主要用来记录开发中两者结合时需要注意的一些事项. 第一步我们 ...

  8. 让Delphi XE5跟其他版本的Delphi共存 [转]

    找到Delphi XE5的安装根目录  ....  \Program Files (x86)\Embarcadero\RAD Studio\12.0\bin下的cglm.ini文件, 打开cglm.i ...

  9. mysqlli 的基本用法

    Mysqli是php5之后才有的功能 需要修改php.ini的配置文件 查找下面的语句: ;extension=php_mysqli.dll 将其修改为:extension=php_mysqli.dl ...

  10. Java面向对象课程设计——购物车

    Java面向对象课程设计——购物车 小组成员:余景胜.刘格铭.陈国雄.达瓦次仁 一.前期调查 流程 客人(Buyer)先在商城(Mall)中浏览商品(Commidity),将浏览的商品加入购物车(Sh ...