版权声明:本文为博主原创文章,未经博主同意不得转载。 https://blog.csdn.net/ClamReason/article/details/23971805

首先重点推荐介绍otl介绍及使用方法的文章:http://blog.csdn.net/rain_qingtian/article/details/12749177
(1)首先安装MySql数据库服务:

下载:http://pan.baidu.com/s/1i3rCnQH

安装步骤:http://write.blog.csdn.net/postedit/23966241

(2)安装navicat数据库client:

下载:http://pan.baidu.com/s/1i3kMOy5

安装步骤:傻瓜安装,选择字符集的时候自己依据情况选择gbk2312,或者utf-8(一般涉及到网络传输或者跨平台,比方和java项目公用数据。会选择utf-8)

(3)安装odbc数据库连接驱动:

下载:http://pan.baidu.com/s/1sjPicjF

安装:http://jingyan.baidu.com/article/8065f87f38b31423312498e4.html 注意这里的安装介绍里面的:打开数据源:開始->设置->控制面板->“管理工具”找到“数据源”

(4)使用otl操作mysql

(4.1)最新封装C++操作otl类库及演示样例代码下载(可直接执行):http://pan.baidu.com/s/1i31bZUX

(4.2)以下的演示样例更加简单,因为没有封装otl,仅仅是使用了全局的otl_connect来实现的,方便入门

创建VS项目:

包括头文件:otl4.h 

下载:http://pan.baidu.com/s/1c0tK1jE

包括源文件:

#include <iostream>
using namespace std; #include <stdio.h>
#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<int>,:f2<char[5]>)",
// 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<<i;
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<<(unsigned char*)tmp;
// overloadedoperator<<(const unsigned char*) in the case of Unicode
// OTL acceptsa pointer to a Unicode character array.
//operator<<(const unsigned short*) wasn't overloaded
// in order toavoid ambiguity in C++ type casting.
} } void select()
{
otl_stream i(50, //buffer size
" select* from test_tab "
"where f1>= :f11<int> "
" and f1 <= :f12<int>*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<<"f1="<<f1<<", f2=";
for(int j=0;f2[j]!=0;++j)
cout<<""<<f2[j];
cout<<endl;
} 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<<"f1="<<f1<<", f2=";
for(int j=0;f2[j]!=0;++j)
cout<<""<<f2[j];
cout<<endl;
} } 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<<p.msg<<endl; // print out error message
cerr<<p.stm_text<<endl; // print out SQL that caused the error
cerr<<p.var_info<<endl; // print out the variable that caused the error
} 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

较好的介绍样例地址:http://www.cnblogs.com/skyme/archive/2010/11/08/1871509.html

语法总结:

连接初始化: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<<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

数据库操作:在数据库的连接和断开之间执行,往往同一个连接内部进行多个数据库操作

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<int>,:f2<char[31]>,:f3<timestamp>)", 
                 // SQL statement
              db // connect object
             );

////

  o<<i<<f2<<f3;

查询

otl_stream i(50, // buffer size
"select * from test_tab where f1>=:f11<int> and f1<=:f12<int>*2",
// SELECT statement
db // connect object
); 

Linux下用OTL操作MySql(包含自己封装的类库及演示样例代码下载)的更多相关文章

  1. Linux下使用OTL操作mysql数据库

    首先重点推荐介绍otl介绍及用法的文章:http://www.cnblogs.com/fnlingnzb-learner/p/5835560.html 一.编写代码 注:以下代码来自OTL示例,略有改 ...

  2. java文件夹相关操作 演示样例代码

    java文件夹相关操作 演示样例代码 package org.rui.io; import java.io.File; import java.io.FilenameFilter; import ja ...

  3. linux下的shell操作mysql

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

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

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

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

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

  6. mysql 存储过程 演示样例代码

    drop procedure if exists P_SEQUENCE; /** 暂省略包 @AUTO LIANGRUI 2014/6/27 T_PRO_PRODUCT 表 排序 对整个表进行按序号排 ...

  7. JDBC连接MySQL数据库及演示样例

    JDBC是Sun公司制定的一个能够用Java语言连接数据库的技术. 一.JDBC基础知识         JDBC(Java Data Base Connectivity,java数据库连接)是一种用 ...

  8. linux下的文本操作之 文本查找——grep

    摘要:你有没有这样的应用场景:调试一个程序,出现debug的提示信息,现在你需要定位是哪个文件包含了这个debug信息,也就是说,你需要在一个目录下的多个文件(可能包含子目录)中查找某个字符串的位置: ...

  9. centOS Linux下用yum安装mysql

    centOS Linux下用yum安装mysql      第一篇:安装和配置MySQL   第一步:安装MySQL   [root@192 local]# yum -y install mysql- ...

随机推荐

  1. Xshell远程连接的具体操作和Xshell多会话设置小技巧

    前几天给大家分享了Xshell的安装教程,今天给大家分享如何在Xshell中进行远程连接,并且分享一下如何设置一条命令可以发送多个终端,这里以Xshell6为例进行说明,具体的教程如下. 1.依次点击 ...

  2. Java String字符串的不可变

    Java 通过把String类设计为final使类不可继承,将变量value设置为private并且是final的,且value没有setter方法,不可修改. 为什么这么设计: 1.字符串常量池的需 ...

  3. 全链路跟踪skywalking简介

    该文章主要包括以下内容: skywalking的简介 skywalking的使用,支持多种调用中间件(httpclent,springmvc,dubbo,mysql等等) skywalking的tra ...

  4. 在.slurm文件中激活Anaconda环境

    超算中心使用slurm作为集群调度.原始slurm脚本如下: source activate tensorflow-gpu python neural_style.py --content conte ...

  5. CentOS7 开机启动脚本与命令后台运行

    一.& 在 Linux 命令后加上 &  可以在后台运行 二.nohup 对 SIGHUP 信号免疫,对 SIGINT 信号不免疫,可用 shopt | grep hup 查看. 当关 ...

  6. jquery 复合事件 toggle()方法的使用

    定义和用法 toggle() 方法用于绑定两个或多个事件处理器函数,以响应被选元素的轮流的 click 事件. 语法: $(selector).toggle(function1(),function2 ...

  7. vsftpd 配置上传失败553

    1查看ftp服务 ftp://ftpuser:123456@10.11.102.58:8023 2 getsebool -a | grep ftp 临时关闭 [root@xuegod63~]# get ...

  8. 转自网络用LIBSVM进行回归预测的粗浅认识————————作者师梦

    说一说我对用LIBSVM进行回归预测的粗浅认识(整理完成于2012年5.11) 作者 :  师梦       吾本工程小硕一枚,前用matlab建模,已然完成.某日,正沾沾自喜之际,吾师曰:“汝已为之 ...

  9. EXCEL中自定义格式输入的数据怎么完整复制

    在用设置单元格式里 自定义 输入数值 如图,B列的数据,我复制后,用选择性粘贴到别的地方,还是无法将75FG4Y2一起复制过去,只能复制过去FG 怎么办? ===>先把这些复制到一个记事本里,再 ...

  10. WebView的用法

    基本用法 布局文件配置WebView <WebView android:id="@+id/wv_news_detail" android:layout_width=" ...