C++ otlv4 通过odbc连接 sql server 数据库


otl 简介

otl是一个纯C++的通用数据库连接模板库,可以支持各种当下流行的数据库,如oracle,sybase,mysql,postgresql等。它是一个跨平台类库,在Win,unix上都可使用。otl使用非常简单,只要头文件中包含有:#include "otlv4.h" 即可,实际上整个otl就一个.h 文件,使用起来极为方便。otl源文件下载地址:http://otl.sourceforge.net/

otl 常用类

otl常用类主要包括:otl_stream、otl_connect、otl_exception。

  • olt_stream类:otl 流概念的具体表现形式,任何输入、输出参数,sql语句调用等,在C++编程过程中都能使用otl_stream类实现。
  • otl_connect类:封装了一系列相关数据库连接的功能:建立连接、断开连接、事务提交、事务回滚等等。otl_connect是C++编程中创建和使用数据库连接以及进行数据库事务管理的类。
  • otl_exception类:用于描述otl操作数据时抛出的异常。

otl 测试代码

//otltest.h

#pragma once
#ifndef _OTLTEST_H 
#define _OTLTEST_H 
#include <iostream>
using namespace std;

#include <stdio.h>

#define OTL_ODBC_MSSQL_2008
#include "otlv4.h"

class OtlTest
{
public:
    OtlTest();
    ~OtlTest();
    void insertTest(const char* insertsql);
    void selectTest(const char* selectsql);
    void connectTest(const char* connectsql);
    void dropTest(const char* dropsql);
    void createTest(const char* createsql);

    void init();
    void uninit();
private:
    otl_connect m_db; //connect object
};

#endif	// _OTLTEST_H

//otltest.cpp
#include "OtlTest.h"



OtlTest::OtlTest()
{
}


OtlTest::~OtlTest()
{
}

void OtlTest::init() {
    // initialize odbc environment
    otl_connect::otl_initialize();
}

void OtlTest::uninit() {
    //disconnect from the database
    m_db.logoff();
}

void OtlTest::insertTest(const char* insertsql) {
    //insert rows into table
    otl_stream o(10, //buffer size
        insertsql, //sql statement
        m_db //connect object
    );
    o.set_commit(0); //set stream's auto-commit to off

    try
    {
        o << 1 << "Line1"; //Enter one row into the stream
        o.flush(); //flush the stream buffer,i.e. force
                   //the stream to execute
#if defined(OTL_ANSI_CPP_11_VARIADIC_TEMPLATES)
        otl_write_row(o, 1, "Line1");
#else
                   // when variadic template functions are not supported by the C++
                   // compiler, OTL provides nonvariadic versions of the same template
                   // functions in the range of [1..15] parameters
        otl_write_row(o, 1, "Line1");

        // the old way (operators >>() / <<()) is available as always:
        // o<<1<<"Line1"; // Enter the same data into the stream
        // and cause a "duplicate key" error.
#endif
        o.flush();
    }
    catch (otl_exception& p)
    {
    	if (p.code == 2601)
    	{
            //...duplicate key ...
            cout << "STREAM ERROR STATE=" << o.get_error_state() << endl;
            o.clean(1); // clean up the stream's buffer
                        // and clean up the stream's internal
                        // error flag as well. By doing this,
                        // it's possible to recover from
                        // a database error without closing
                        // the stream. Remember, the number of
                        // situtation when it's possible is
                        // limited and the recovery procedure should
                        // be carefully designed.
    	}
        else
        {
            throw;
        }
    }
    
    o << 2 << "Line2"; //Enter one more row of data
                       //after recovering from the "duplicate key" error
    o.flush();
    m_db.commit(); //commit transation
}

void OtlTest::selectTest(const char* selectsql)
{
    otl_stream i(10, //buffer size
        selectsql, //select statement
        m_db //connect object
    );

    int num1;
    char ch2[31];

    for (auto& it:i)
    {
        otl_read_row(it, num1, ch2);
        cout << "num1 = " << num1 << ", ch2 = " << ch2 << endl;
    }
}

void OtlTest::connectTest(const char* connectsql)
{
    m_db.rlogon(connectsql);
}

void OtlTest::dropTest(const char* dropsql)
{
    otl_cursor::direct_exec(
        m_db,
        dropsql,
        otl_exception::disabled
    ); //drop table
}

void OtlTest::createTest(const char* createsql)
{
    otl_cursor::direct_exec(
        m_db,
        createsql
    ); //create table
}

//main.cpp
#include "OtlTest.h"

using namespace pro;

string connectsql = "DSN=sql_test;UID=sa;PWD=yusur2019;database=Record";
string creatsql = "create table test_tab(num1 int, ch2 varchar(30))";
string dropsql = "drop table test_tab";
string selectsql = "select * from test_tab";
string insertsql = "insert into test_tab values(:f1<int>,:f2<char[31]>)";
int main()
{
    //_STACKTRACE_begin_;
    
    //JsonTest jt;
    //jt.printTest(); //Json test.
    
    OtlTest ot;
    ot.init();
    ot.connectTest(connectsql.c_str());
    ot.dropTest(dropsql.c_str());
    ot.createTest(creatsql.c_str());
    ot.insertTest(insertsql.c_str());
    ot.selectTest(selectsql.c_str());
    ot.uninit();

    //_STACKTRACE_end_;
    system("pause");
    return 0;
}

otl 参考文献

源文件下载地址:http://otl.sourceforge.net/

C++ otlv4 连接 sql server 数据库小记: https://www.bbsmax.com/A/gAJGg6MoJZ/

发布了88 篇原创文章 · 获赞 16 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/WHEgqing/article/details/104538448
今日推荐