└─(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
└─(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
└─(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
vim /usr/local/etc/odbcinst.ini
[DM8] Description = dm odbc Driver = /home/frank/dmdbms/bin/libdodbc.so
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
在.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>
#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 TABLE gchdb.company ( id integer NOT NULL, name character varying(20), age integer NOT NULL, address character(25), salary numeric(18,2), CONSTRAINT )
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