下载及安装数据库
安装数据库驱动
需要到达梦和人大金仓官网注册账号,下载试用版即可。
通过下载包里面的install.bin文件进行安装,安装时选择client模式,因为只需要连接数据库的驱动文件。
安装ODBC驱动
sudo apt install unixodbc unixodbc-dev unixodbc-bin
修改配置文件
设置驱动
vi /etc/odbcinst.ini
[DM7]
Description = dm odbc
Driver = /home/yu/dmdbms/bin/libdodbc.so
[KingbaseES 7 ODBC Driver]
Description = KingbaseES 7 ODBC Driver for Linux
Driver = /home/yu/kingbase7/odbc/kdbodbc7.so
Setup = /home/yu/kingbase7/odbc/kdbodbc7S.so
UsageCount = 1
设置连接
vi /etc/odbc.ini
[dm]
Description = dm7 dsn
Driver = DM7
SERVER =192.168.4.103
UID = SYSDBA
PWD = 123456789
TCP_PORT = 5236
[kingbaseES]
Description = KingbaseES ODBC Data Source
Driver = KingbaseES 7 ODBC Driver
Database = SAMPLES
Servername = 192.168.4.100
Username = system
Password = 123456789
Port = 54321
MAXCONNECT = 512
Fetch = 100
MaxVarcharSize = 254
MaxTextSize = 8000
LFConversion = 0
CaseSensitive = 0
ClientEncoding = UTF-8
UseServerPrepare = 1
ExecutePreparedStatement = 1
UseServerCursor = 0
连接数据库
指定动态链接库
export LD_LIBRARY_PATH=/home/yu/dmdbms/bin:/home/yu/kingbase7/lib:/home/yu/kingbase7/bin:/home/yu/kingbase7/odb
通过工具连接
连接dm数据库
isql dm
连接人大金仓数据库
isql kingbaseES
通过程序连接
odbc 编程参考odbc文档:http://www.unixodbc.org/doc/ProgrammerManual/Tutorial/
#include
#include
#include
#include
int main() {
SQLHENV sqlhenv;
SQLHDBC sqlhdbc;
char stat[10];
SQLINTEGER err, rowanz,id;
SQLSMALLINT mlen;
char msg[200],buffer[200];
int res, ret = -1;
res = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sqlhenv);
if (res != SQL_SUCCESS && res != SQL_SUCCESS_WITH_INFO) {
printf("error alloc env\n");
goto _exit;
}
res = SQLSetEnvAttr(sqlhenv, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3, 0);
if (res != SQL_SUCCESS && res != SQL_SUCCESS_WITH_INFO) {
printf("error set env env\n");
goto _free_env;
}
res = SQLAllocHandle(SQL_HANDLE_DBC, sqlhenv, &sqlhdbc);
if (res != SQL_SUCCESS && res != SQL_SUCCESS_WITH_INFO) {
printf("error all dbc env\n");
goto _free_env;
}
SQLSetConnectAttr(sqlhdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER *)5, 0);
res = SQLConnect(sqlhdbc, (SQLCHAR *)"kingbaseES", SQL_NTSL, (SQLCHAR *)"", SQL_NTSL, \
(SQLCHAR *)"", SQL_NTSL);
if (res != SQL_SUCCESS && res != SQL_SUCCESS_WITH_INFO) {
SQLGetDiagRec(SQL_HANDLE_DBC, sqlhdbc,1,
(SQLCHAR *)stat, &err, (SQLCHAR *)msg, 100, &mlen);
printf("%s (%d)\n",msg, err);
goto _free_dbc;
}
ret = 0;
_free_dbc:
SQLFreeHandle(SQL_HANDLE_DBC, sqlhdbc);
_free_env:
SQLFreeHandle(SQL_HANDLE_ENV, sqlhenv);
_exit:
return ret;
}