首先重点推荐介绍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. Pycharm乱码解决

    现象:输出栏出现乱码 解决方案: 结果:

  2. JS判断字符串是否为空或是否全为空格

    var test = " "; //为空或全部为空格 if (test.match(/^[ ]*$/)) { console.log("all space or empt ...

  3. [转]Aroon Indicator

    Aroon Indicator Trend Oscillator Description The Aroon indicator, developed by Tushar Chande, indica ...

  4. 虚拟机ubuntu新增挂载点进行磁盘扩展

    参考: http://m.blog.csdn.net/blog/pcsxk/38501579 一.vmware下扩展原来的磁盘空间 这个比较直观 1.关机状态下,选择磁盘->实用工具->扩 ...

  5. Video Test Pattern Generator(7.0)软件调试记录

    Video Test Pattern Generator(7.0)软件调试记录 . XVidC_VideoMode XVIDC_VM_576_50_I = XVIDC_VM_720x576_50_I ...

  6. Centos 使用find查找

    CentOS查找目录或文件 find / -name svn 查找目录:find /(查找范围) -name '查找关键字' -type d查找文件:find /(查找范围) -name 查找关键字 ...

  7. mysql 闪回原理

    利用MySQL闪回技术恢复误删除误更改的数据 笔者相信很多人都遇到过忘带where条件或者where条件漏写了一个和写错了的情况,结果执行了delete/update后把整张表的数据都给改了.传统的解 ...

  8. 黄聪:JQUERY判断操作CHECKBOX选中、取消选中、反选、第二次无法选中的问题

    用JQuery做CheckBox全选和反选的时候,遇到一个问题.当用JQ控制全选,全取消一次以后,再次点击全选,发现代码变了,但是CheckBox没有处于选中状态. $("#id" ...

  9. pytest.7.常见套路

    From: http://www.testclass.net/pytest/common_useage/ 在使用pytest的时候,下面这些问题我们可能会经常遇到,这里给出官方的解决方案,按照套路来执 ...

  10. PAT 乙级 1049 数列的片段和(20) C++版

    1049. 数列的片段和(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CAO, Peng 给定一个正数数列,我们可以从中截 ...