otl通过myodbc连接mysql

来源:互联网 发布:北京碧水源 知乎 编辑:程序博客网 时间:2024/05/17 06:04

Otl简介

OTL 是 Oracle,Odbc andDB2-CLI Template Library 的缩写,是一个C++编译中操控关系数据库的模板库,它目前几乎支持所有的当前各种主流数据库,例如Oracle,MS SQL Server,Sybase,Informix,MySQL,DB2,Interbase /Firebird,PostgreSQL,SQLite,SAP/DB,TimesTen,MS ACCESS等等。OTL中直接操作Oracle主要是通过Oracle提供的OCI接口进行,进行操作DB2数据库则是通过CLI接口来进行,至于MS的数据库和其它一些数据库,则OTL只提供了ODBC来操作的方式。当然Oracle和DB2也可以由OTL间接使用ODBC的方式来进行操纵。

Myodbc

因为otl只能通过odbc连接mysql,所以首先需要安装myodbc,myodbc官网下载地址:http://www.mysql.com/downloads/connector/odbc/,根据自己的电脑下载,我下载的是

Connector/ODBC5.1.10 Windows (x86, 32-bit), MSI Installer Connector-ODBC,然后安装即可,点击默认安装就行了。

添加数据源

打开控制面板-》点击管理工具-》点击数据源,然后添加数据源

打开ODBC数据源管理器对话框,如下图所示:

1.    ODBC数据源管理器。(注:在64位系统中装32mysqlodbc,启动C:\Windows\SysWOW64\odbcad32.exe


2.    ODBC数据源管理器对话框中,点击添加。打开创建新数据源对话框。

3.    选择MySQL ODBC 5.1驱动程序,然后点击完成打开“MySQLODBC 5.1驱动程序-DSN配置对话框,如下图所示:


4.    数据源名框中,输入打算访问的数据源的名称。它可以是你选择的任何有效名称。


5.    描述框中,输入DSn所需的描述信息。


6.    主机服务器名(或IP)框中,输入准备访问的MySQL服务器主机的名称。默认情况下为localhost(本地主机)。

7.    数据库名框中,输入准备用作默认数据库的MySQL数据库名称。

8.    用户框中,输入你的MySQL用户名(数据库用户ID)。

9.    密码框中输入密码。

10.端口框中,如果端口不是默认端口,输入端口号。

11.选择一个数据库吧


点击ok,添加数据源完毕。

12.修改字符集名称,否则会出现乱码

下载otl

http://otl.sourceforge.net/otl3_down.htm下载地址,


下载头文件即可。

测试一下

当然首先要把otl头文件拷贝到工程目录,链接的时候要用,下面是一个简单的测试代码,如果刚才选择了一个数据库的话,就在当前的数据库中添加了一个表;

#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.        "insertinto 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<<(unsignedchar*)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 "        "wheref1>=: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>>(unsignedchar*)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{         db.rlogon("root/root@myodbc5");// connect to the database         otl_cursor::direct_exec            (            db,            "droptable test_tab",            otl_exception::disabled // disable OTL exceptions            ); // droptable         otl_cursor::direct_exec            (            db,            "createtable 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; }


要注意的是更改db.rlogon("root/root@myodbc5");// connect to the database

其中的数据库的用户名和密码,还有数据源的名称。

运行的结果

f1=8, f2= 1111 2222 3333 4444
f1=9, f2= 1111 2222 3333 4444
f1=10, f2= 1111 2222 3333 4444
f1=11, f2= 1111 2222 3333 4444
f1=12, f2= 1111 2222 3333 4444
f1=13, f2= 1111 2222 3333 4444
f1=14, f2= 1111 2222 3333 4444
f1=15, f2= 1111 2222 3333 4444
f1=16, f2= 1111 2222 3333 4444
f1=4, f2= 1111 2222 3333 4444
f1=5, f2= 1111 2222 3333 4444
f1=6, f2= 1111 2222 3333 4444
f1=7, f2= 1111 2222 3333 4444
f1=8, f2= 1111 2222 3333 4444

原创粉丝点击