c++ 通过ODBC访问达梦数据库DM8

2022/3/19 2:28:24

本文主要是介绍c++ 通过ODBC访问达梦数据库DM8,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

安装ODBC

测试环境

└─(22:07:32)──> cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.4 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.4 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

下载unixODBC

└─(22:10:02)──> wget -c ftp://ftp.unixodbc.org/pub/unixODBC/unixODBC-2.3.9.tar.gz
--2022-03-18 22:10:05--  ftp://ftp.unixodbc.org/pub/unixODBC/unixODBC-2.3.9.tar.gz
           => “unixODBC-2.3.9.tar.gz”
正在解析主机 ftp.unixodbc.org (ftp.unixodbc.org)... 87.106.19.214
正在连接 ftp.unixodbc.org (ftp.unixodbc.org)|87.106.19.214|:21... 已连接。
正在以 anonymous 登录 ... 登录成功!
==> SYST ... 完成。    ==> PWD ... 完成。
==> TYPE I ... 完成。  ==> CWD (1) /pub/unixODBC ... 完成。
==> SIZE unixODBC-2.3.9.tar.gz ... 1676145
==> PASV ... 完成。    ==> RETR unixODBC-2.3.9.tar.gz ... 完成。
长度:1676145 (1.6M) (非正式数据)

unixODBC-2.3.9.tar.gz                100%[====================================================================>]   1.60M   105KB/s    用时 25s   

2022-03-18 22:10:35 (66.6 KB/s) - “unixODBC-2.3.9.tar.gz” 已保存 [1676145]

编译&安装

tar -zxvf unixODBC-2.3.9.tar.gz
cd unixODBC-2.3.9
./configure 
make -j4
sudo make install

配置odbc DSN

查看配置信息

└─(22:15:19)──> odbcinst -j 
unixODBC 2.3.9
DRIVERS............: /usr/local/etc/odbcinst.ini
SYSTEM DATA SOURCES: /usr/local/etc/odbc.ini
FILE DATA SOURCES..: /usr/local/etc/ODBCDataSources
USER DATA SOURCES..: /home/frank/.odbc.ini
SQLULEN Size.......: 8
SQLLEN Size........: 8
SQLSETPOSIROW Size.: 8

编辑odbc驱动

vim /usr/local/etc/odbcinst.ini

[DM8]
Description = dm odbc
Driver = /home/frank/dmdbms/bin/libdodbc.so

编辑ODBC数据源

vim /usr/local/etc/odbc.ini

[dm]
Description = gch for DM8
Driver = DM8
Trace = yes
TraceFile = sql.log
SERVER= localhost
UID= SYSDBA
PWD = SYSDBA
TCP_PORT= 5236

测试ODBC链接

环境变量

.bash_profile最后增加环境变量
vim ~/.bash_profile

export LD_LIBRARY_PATH=/dm8/dmdbms/bin:$LD_LIBRARY_PATH

使环境变量生效

source ~/.bash_profile

链接达梦

└─(22:26:20)──> isql -v dm
+---------------------------------------+
| Connected!                            |
|                                       |
| sql-statement                         |
| help [tablename]                      |
| quit                                  |
|                                       |
+---------------------------------------+
SQL> select id_code;
+----------------------------------------------------------------------------+
| ID_CODE                                                                                                                         |
+----------------------------------------------------------------------------+
| 1-1-126-20.09.04-126608-ENT                                                                                                     |
+----------------------------------------------------------------------------+
SQLRowCount returns 1
1 rows fetched
SQL> 

编写C++代码

otlodbc.cpp

#include <iostream>
using namespace std;
#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#include <stdio.h>


#define OTL_ODBC // CompileOTL 4.0/ODBC  
#define OTL_ODBC_UNIX // uncomment this line if UnixODBC is used

#include "otlv4.h" // include the OTL 4.0 header file

otl_connect db; // connect object

void insert()
{
    try
    {
        otl_nocommit_stream o;
        o.open(50, // buffer size
            "insert into SYSDBA.COMPANY(ID, NAME, AGE, ADDRESS, SALARY) \
            VALUES(:id<int>, :name<char[21]>, :age<int>, :address<char[26]>, :salary<float>);",
            // SQL statement
            db // connect object
        );
        
        o.set_flush(false);
        o.set_commit(1);

        char name[50] = {"test_name"}; 
        name[20] = '\0';
        o << 100 << name << 31 << "test_addr1" << static_cast<float>(10000);
        o << 101 << name << 32 << "test_addr2" << static_cast<float>(10000);

        o.flush();
        // db.commit();
    }
    catch(otl_exception& p)
    {
        cout<<"otl_exception:"<<endl;
        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
    }

}  

void select()
{ 
    try{
        otl_stream ostream1(500, // buffer size
            "select * from company;",// SELECT statement
            db // connect object
            ); 
        // create select stream

        int id;
        int age;
        unsigned char name[255];
        unsigned char address[255];
        double salary;

        while(!ostream1.eof())
        { // while not end-of-data
            ostream1>>id;
            ostream1>>name;
            ostream1>>age;
            ostream1>>address;
            ostream1>>salary;
            cout<<"id="<<id<<endl;
            cout<<"age="<<age<<endl;
            cout<<"name="<<name<<endl;
            cout<<"address="<<address<<endl;
        }
    }
    catch(otl_exception& p)
    { // intercept OTL exceptions
        cout<<"otl_exception:"<<endl;
        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
    }
}

int main()
{
    otl_connect::otl_initialize(); // initialize the database API environment
    try{
        db.auto_commit_off();
        db.rlogon("DSN=dm;UID=SYSDBA;PWD=SYSDBA;database=gchdb"); // connect to the database
        insert();
        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

    return 0;

}

create_test_table.sql

CREATE TABLE gchdb.company
(
	    id integer NOT NULL,
	    name character varying(20), 
	    age integer NOT NULL,
	    address character(25), 
	    salary numeric(18,2),
	    CONSTRAINT 
)

CMakeLists.txt

cmake_minimum_required (VERSION 3.11)
project (otlodbc)

set(CMAKE_CXX_FLAGS "-Wall")
# set(CMAKE_CXX_FLAGS "-Wall -DOTL_ODBC_UNIX")
set(CMAKE_CXX_FLAGS_DEBUG "-g3")
set(CMAKE_CXX_FLAGS_RELEASE "-O2")
set(CMAKE_BUILD_TYPE Debug)

include_directories(./)
include_directories(/usr/local/include)

add_executable(otlodbc otlodbc.cpp)
link_directories("/home/frank/dmdbms/bin")
target_link_libraries(otlodbc /home/frank/dmdbms/bin/libdodbc.so)%

编译

mkdir build 
cd build
cmake ../
make -j4

运行

id=101
age=32
name=test_name
address=test_addr2


这篇关于c++ 通过ODBC访问达梦数据库DM8的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程