linux otl mysql_Linux下用OTL操作MySql(包含自己封装的类库及演示样例代码下载)...
(1)首先安装MySql数据库服务:
(2)安装navicat数据库client:
安装步骤:傻瓜安装,选择字符集的时候自己依据情况选择gbk2312,或者utf-8(一般涉及到网络传输或者跨平台,比方和java项目公用数据。会选择utf-8)
(3)安装odbc数据库连接驱动:
(4)使用otl操作mysql
(4.1)最新封装C++操作otl类库及演示样例代码下载(可直接执行):http://pan.baidu.com/s/1i31bZUX
(4.2)以下的演示样例更加简单,因为没有封装otl,仅仅是使用了全局的otl_connect来实现的,方便入门
创建VS项目:
包括头文件:otl4.h
包括源文件:
#include
using namespace std;
#include
#define OTL_ODBC // CompileOTL 4.0/ODBC
// Thefollowing #define is required with MyODBC 5.1 and higher
#define OTL_ODBC_SELECT_STM_EXECUTE_BEFORE_DESCRIBE
#define OTL_UNICODE // CompileOTL with Unicode
#include "otlv4.h"// include the OTL 4.0 header file
otl_connect db; // connect object
void insert()
// insert rowsinto table
{
otl_stream o(1, //buffer size should be == 1 always on INSERT.
"insert into test_tab values(:f1,:f2)",
// SQLstatement, char[5] means 5 2-byte
// Unicodecharatcters including a null
// terminator
db // connectobject
);
unsigned short tmp[32]; // Nullterminated Unicode character array.
for(int i=1;i<=100;++i){
o<
tmp[0]=1111; //Unicode character (decimal code of 1111)
tmp[1]=2222; //Unicode character (decimal code of 2222)
tmp[2]=3333; //Unicode chracater (decimal code of 3333)
tmp[3]=4444; //Unicode chracater (decimal code of 4444)
tmp[4]=0; //Unicode null terminator
o<
// overloadedoperator<
// OTL acceptsa pointer to a Unicode character array.
//operator<
// in order toavoid ambiguity in C++ type casting.
}
}
void select()
{
otl_stream i(50, //buffer size
" select* from test_tab "
"where f1>= :f11 "
" and f1 <= :f12*2 ",
// SELECTstatement
db // connectobject
);
// create selectstream
int f1;
unsigned short f2[32];
i<<8<<8; // assigning :f11 = 8, f12 = 8
// SELECTautomatically executes when all input variables are
// assigned. Firstportion of output rows is fetched to the buffer
while(!i.eof()){// while not end-of-data
i>>f1;
i>>(unsigned char*)f2;
// overloaded operator>>(unsignedchar*) in the case of Unicode
// OTL acceptsa pointer to a Unicode chracter array.
//operator>>(unsigned short*) wasn't overloaded
// in order toavoid ambiguity in C++ type casting.
cout<
for(int j=0;f2[j]!=0;++j)
cout<
cout<
}
i<<4<<4; // assigning :f11 = 4, :f12 = 4
// SELECTautomatically executes when all input variables are
// assigned. Firstportion of output rows is fetched to the buffer
while(!i.eof()){// while not end-of-data
i>>f1>>(unsigned char*)f2;
cout<
for(int j=0;f2[j]!=0;++j)
cout<
cout<
}
}
int main()
{
otl_connect::otl_initialize(); // initialize the database API environment
try{
// connect to the database user/psw/dsn,这里的dsn是odbc创建数据源的时候设置的,
//注意dsn是odbc连接的名字。不是数据库的名字。otl是通过odbc的名字找到数据库的,
//而这个名字对于的配置里面
//已经包括了IP,端口等信息,仅仅要你提供username和password就能够訪问了
//见http://jingyan.baidu.com/article/8065f87f38b31423312498e4.html
db.rlogon("root/123456@local_connect");
otl_cursor::direct_exec
(
db,
"drop table test_tab",
otl_exception::disabled // disable OTL exceptions
); // droptable
otl_cursor::direct_exec
(
db,
"create table test_tab(f1 int, f2 varchar(11))"
); // create table
insert(); //insert records into table
select(); //select records from table
}
catch(otl_exception&p){ // intercept OTL exceptions
cerr<
cerr<
cerr<
}
db.logoff(); //disconnect from the database
getchar();
return 0;
}
输出:
f1=8, f2=1111222233334444
f1=9, f2=1111222233334444
f1=10, f2=1111222233334444
f1=11, f2=1111222233334444
f1=12, f2=1111222233334444
f1=13, f2=1111222233334444
f1=14, f2=1111222233334444
f1=15, f2=1111222233334444
f1=16, f2=1111222233334444
f1=4, f2=1111222233334444
f1=5, f2=1111222233334444
f1=6, f2=1111222233334444
f1=7, f2=1111222233334444
f1=8, f2=1111222233334444
语法总结:
连接初始化:otl_connect::otl_initialize();
连接对象:otl_connect db; // connect object
连接数据库: db.rlogon("UID=scott;PWD=tiger;DSN=postgresql"); // connect to ODBC
连接数据库一般放在try子句中:
try{
db.rlogon("UID=scott;PWD=tiger;DSN=postgresql"); // connect to ODBC
}
catch(otl_exception& p){ // intercept OTL exceptions
cerr<
cerr<
cerr<
cerr<
}
db.logoff(); // disconnect from ODBC
数据库操作:在数据库的连接和断开之间执行,往往同一个连接内部进行多个数据库操作
db.rlogon("UID=scott;PWD=tiger;DSN=postgresql"); // connect to ODBC
otl_cursor::direct_exec
(
db,
"drop table test_tab",
otl_exception::disabled // disable OTL exceptions
); // droptable
表的创建
string sql ="create table test_tab(f1 int, f2 varchar(11))";
//create table person_tab(age int, student_name char(30))
otl_cursor::direct_exec
(
db,
sql.c_str();
); // create table
表的删除
otl_cursor::direct_exec
(
db,
"drop table test_tab",
otl_exception::disabled // disable OTL exceptions
); // droptable
插入
otl_stream o(50, // buffer size
"insert into test_tab "
"values(:f1,:f2,:f3)",
// SQL statement
db // connect object
);
o<
查询
otl_stream i(50, // buffer size
"select * from test_tab where f1>=:f11 and f1<=:f12*2",
// SELECT statement
db // connect object
);
linux otl mysql_Linux下用OTL操作MySql(包含自己封装的类库及演示样例代码下载)...相关推荐
- linux hive mysql_Linux下的Hive与Mysql安装
一,安装MySQL sudo apt-get install mysql-server mysql-client 1). 建立数据库hive, create database hive; 2). 创建 ...
- JDBC连接MySQL数据库及演示样例
JDBC是Sun公司制定的一个能够用Java语言连接数据库的技术. 一.JDBC基础知识 JDBC(Java Data Base Connectivity,java数据库连接)是一种用 ...
- linux python开发环境sql数据迁移到mysql_linux环境下python怎样操作mysql数据库
展开全部 linux环境下python怎样操作mysql数据库呢?方法如下: 首先在Linux环境下安装mysql-python 1.下载mysql-python 打开终端: cd /usr/loca ...
- linux下使用python操作mysql
linux下使用python操作mysql 文章目录 linux下使用python操作mysql 一.Python 中MySQL的几个模块对象 1.模块 2.Connection 对象 3.Curso ...
- linux下使用C++操作mysql
linux下使用C++操作mysql 文章目录 linux下使用C++操作mysql 一.常用类型介绍 1. MYSQL 2. MYSQL_ROW 3. MYSQL_FIELD 4. MYSQL_RE ...
- linux下c语言调用mysql,Linux下C语言操作MYSQL总结
Linux下C语言操作MYSQL总结 发布时间:2006-12-17 00:40:00来源:红联作者:anciens 下文的用到的数据库表: 数据库中存在表table, 有name(varchar类型 ...
- MySql的事务操作与演示样例
事务就是一个逻辑工作单元的一系列步骤. 事务是用来保证数据操作的安全性 事务的特征: Atomicity(原子性) Consistency(稳定性,一致性) Isolation(隔离性) Durabi ...
- Python 操作MySql数据库(封装、优雅)
Python 记录操作MySql数据库(封装)--优雅 学了pymysql第三方库(pip install pymysql)来操作MySql数据库后,浅记一下对MySql进行 <关于我的MySq ...
- myloader mysql_myloader恢复mysql数据库演示样例
? mydumper是针对mysql数据库备份的一个轻量级第三方的开源工具.备份方式为逻辑备份.它支持多线程.备份速度远高于原生态的mysqldump以及众多优异特性.与其相配套的恢复工具则是mylo ...
- linux otl 连接mysql_Linux下用OTL操作MySql(包括自己封装的类库及示例代码下载)...
(1)首先安装MySql数据库服务: (2)安装navicat数据库客户端: 安装步骤:傻瓜安装,选择字符集的时候自己根据情况选择gbk2312,或者utf-8(一般涉及到网络传输或者跨平台,比如和j ...
最新文章
- 干货,Wireshark使用技巧-过滤规则
- eclipse+adt+android sdk,64位Ubuntu 11.10下Android开发环境的搭建(JDK+Eclipse+ADT+Android SDK详细)...
- 腾讯面试:打家劫舍 III
- 用 Go 语言实现 Raft 选主
- android avd orientation support,Android AVD-无法旋转风景/人像
- JWT生成Token做登录校验讲解
- 【QGIS入门实战精品教程】3.3:QGIS如何打开ArcGIS创建的文件数据库(GDB)?
- [vue] ajax、fetch、axios这三都有什么区别?
- 今天的我家有12口人的即时通讯
- wxPython4.0中ListCtrl入门
- 《循序渐进Oracle:数据库管理、优化与备份恢复》一一第1章 Oracle数据库的创建...
- PWM驱动sg90电机
- ios 网速监控_iOS 监测网络状态
- iOS中 断点下载详解 韩俊强的博客
- 电机位置、速度检测方法大合集
- Window 10未连接到互联网
- Sherlock之Instructions指令介绍(Sherlock Version: 7.2.5.1 64-bit)
- 科技云报道:从Robot到Cobot,人机共融正在开创一个时代
- OSPF基本工作原理(上)
- 机器学习中的聚类算法有哪几种?
热门文章
- Jenkins自动集成
- 计算机音量控制器打不开,Win10系统音量控制打不开的解决方法
- 计算机箱图和结构,新手训练营:揭秘“元五金”机箱结构
- cadence 通孔焊盘_Allegro PCB -通孔焊盘制作 及Flash制作
- IT技术人,不可有傲气,但须有傲骨
- 【jQuery】jQuery本地时间与世界时间时差转换
- 配置管理的目标和主要活动
- 手机logging什么意思_手机logging怎么关闭
- 新能源汽车控制技术分享:VCU整车控制器电控开发
- Scrum板与Kanban如何抉择?ivhbyfphe板与按照drpxcj