安装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
复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
文章被以下合辑收录
评论
相关阅读
达梦数据发布上市后首份财报:2024年营收10.4亿、净利3.6亿,销售平均薪酬 101 万、研发 36 万
通讯员
210次阅读
2025-04-15 12:09:47
国产数据库图谱又上新|82篇精选内容全览达梦数据库
墨天轮编辑部
184次阅读
2025-04-23 12:04:21
全国首部图数据库国家标准发布!达梦数据深度参与!
达梦数据
157次阅读
2025-04-02 09:34:13
达梦数据携手中国移动建成国内最大分布式数据库集群
通讯员
149次阅读
2025-04-02 15:10:38
达梦数据库快速上手指南
孙莹
131次阅读
2025-04-10 23:35:47
SQLark V3.4 更新 | 新增 PostgreSQL 数据库支持、SQL 常用代码段、表设计器体验升级,超多新功能等你来探索!
达梦产品与服务
116次阅读
2025-04-17 09:38:11
达梦中国数据库产业基地竣工,光谷崛起“数据之弧”
通讯员
114次阅读
2025-04-23 09:41:05
SQLark 数据生成 | 外键、自增列、check约束、虚拟列都能自动识别!
达梦产品与服务
94次阅读
2025-04-11 10:41:05
【喜报】您有一件邮件请查收!恭喜您通过达梦DCA/DCP考试~
云贝19941464235
67次阅读
2025-04-02 10:47:26
中国软件深度研究报告:打造操作系统、数据库央企龙头
通讯员
33次阅读
2025-04-21 12:20:29