otl通过myodbc连接mysql

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

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
OTL(Ov{er} The Last Version 4)是一个开源的库,用于在C++中连接和操作数据库。我将使用OTLv4连接MySQL数据库。 首先,我们需要准备好OTLv4库和MySQL的驱动程序。我们可以从OTL官方网站下载OTLv4库,并且从MySQL官方网站下载MySQL的驱动程序。确保将这些文件保存在适当的目录中,并设置好编译器的相关设置。 接下来,我们需要包含OTL头文件并初始化OTL库。在代码中添加以下代码: #include <otlv4.h> int main() { // 初始化OTLotl_connect::otl_initialize(); // 连接MySQL数据库 otl_connect db; try { // 使用MySQL驱动程序连接数据库 db.rlogon("mysql_user_name/mysql_password@mysql_data_source"); // 这里的mysql_user_name是你的MySQL用户名, // mysql_password是你的MySQL密码, // mysql_data_source是你的MySQL数据源地址。 // 执行查询或操作 // 断开数据库连接 db.disconnect(); } catch (otl_exception& e) { // 处理异常 std::cerr << "OTL Error: " << e.msg << std::endl; } // 清理OTLotl_connect::otl_terminate(); return 0; } 以上代码演示了如何使用OTLv4连接MySQL数据库。其中,otl_connect类用于表示数据库连接otl_exception类用于处理异常。在try块中,我们使用驱动程序的rlogon函数连接数据库,并可以在此处执行查询或操作。最后,在catch块中,我们可以处理发生的异常。在代码的结尾处,我们清理OTL库以释放资源。 通过以上步骤,我们就可以使用OTLv4库在C++中连接MySQL数据库

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值