Linux下使用OTL操作mysql数据库
首先重点推荐介绍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数据库的更多相关文章
- Linux下用OTL操作MySql(包含自己封装的类库及演示样例代码下载)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/ClamReason/article/details/23971805 首先重点推荐介绍otl介绍及使 ...
- Linux下使用Python操作MySQL数据库
安装mysql-python 1.下载mysql-python 打开终端: cd /usr/local sudo wget http://nchc.dl.sourceforge.net/sourcef ...
- Linux下C语言操作MySQL数据库
MySQL是Linux系统下广泛使用的开源免费数据库,是Linux应用程序数据存储的首选. Ubuntu下安装 […]
- 在linux下安装并配置mysql数据库
在linux下安装并配置mysql数据库 工具/原料 MySql5.6 CentOS 方法/步骤 1 查找以前是否安装有mysql,使用下面命令: rpm -qa|grep -i mysql ...
- linux下应用crontab对mysql数据库进行定时备份
linux下应用crontab对mysql数据库进行定时备份 @(编程) mysql数据库提供了备份命令mysqldump,可以结合crontab命令进行定时备份. 我写了一个mysqlbackup. ...
- linux下的shell操作mysql
(1)MySQL的启动 重启了一次服务器后,使用> mysql -u root -p登陆是出现下面的错误: ERROR 2002 (HY000): Can't connect to local ...
- Linux下安装以及使用MySQL数据库
1.官网下载mysql数据库:https://dev.mysql.com/downloads/mysql/ 2.linux 下可直接下载:wget https://cdn.mysql.com//Dow ...
- linux下卸载和安装mysql数据库的方法
1.1 MySQL下载 下载地址:http://www.mysql.com/downloads/mysql/5.5.html#downloads 版本:5.1.68 平台:linux general ...
- Linux下的C#连接Mysql数据库
今天在尝试在 Linux 系统下使用C#连接数据库,发现网上这方面的信息很少,所以就写一篇博客记录一下. Linux下这里使用的是mono. 首先是缺少Mysql.Data.dll这个库的,所以需要安 ...
随机推荐
- Oracle sqlnet.ora配置
Oracle sqlnet.ora配置 sqlnet.ora的作用(官网指出的) www.2cto.com 1.限制客户端访问(如指定客户端域为不允许访问) 2.指定命名方法(local nami ...
- How to implement a windbg plugin
How to implement a windbg plugin Define EXT_CLASS #include "lauxlib.h" class EXT_CLASS : p ...
- laravel多条件查询(and,or嵌套查询)
原生sql select * from homework where (id between 1 and 10 or id between 50 and 70) and complete = 1 an ...
- 怎么理解Python画图中的X,y
X_outliers=np.array([[3.4, 1.3], [3.2, 0.8]]) y_outliers=np.array([0, 0]) 要明白X,y不再是我们高中时候学的x,y轴的坐标:首 ...
- "remote:error:refusing to update checked out branch:refs/heads/master"的解决办法(转)
https://blog.csdn.net/jacolin/article/details/44014775 在使用Git Push代码到数据仓库时,提示如下错误: [remote rejected] ...
- go thrift报错问题--WriteStructEnd
问题 go thrift开发过程中,多个goroutine共用一个client时,报错: panic: runtime error: index out of range goroutine 24 [ ...
- Star Schema and Snowflake Schema
在设计数据仓库模型的时候,最常见的两种是星型模型与雪花模型.选择哪一种需要根据业务需求以及性能的多重考量来定. 星型模型 在星型模型当中,一张事实表被若干张维度表所包围.每一个维度代表了一张表,有主键 ...
- 转 Oracle监听器启动出错:本地计算机上的OracleOraDb11g_home1TNSListener服务启动后又停止了解决方案
今早刚上班.客户打电话过来说系统访问不了,输入用户名.用户号不能加载出来!听到这个问题,第一时间想到的是不是服务器重新启动了,Oracle数据库的相关服务没有启动的原因.查看服务的时候,发现相关的服务 ...
- JavaScript之WebSocket技术详解
概述 HTTP协议是一种无状态协议,服务器端本身不具有识别客户端的能力,必须借助外部机制,比如session和cookie,才能与特定客户端保持对话.这多多少少带来一些不便,尤其在服务器端与客户端需要 ...
- azure 1元试用,如何创建虚拟机等
付了1元后,直接进 https://manage.windowsazure.cn 创建虚拟机即可.