Document Type | Technical Information
Category | Interface/Integration
Document Number | TIITI018
Overview
This explains how to test connection to the Tibero database using the .odbc.ini file in a Unix environment along with sample code.
The .odbc.ini file stores ODBC (Open Database Connectivity) configuration information, registering DSNs (Data Source Names) so that applications can connect to the database.
Method
odbc.ini Configuration
Create the $HOME/.odbc.ini file under the account's home directory and configure it according to your environment.
[ODBC Data Sources] tibero5 = Tibero5 ODBC driver [ODBC_TEST] Trace = 1 TraceFile = /home/tibero/iodbc/tmp/odbc.trace [tibero5] Driver = /home/tibero/tibero5/client/lib/libtbodbc.so Description = Tibero5 ODBC Datasource SID = fintac1 User = tibero
Check tbdsn.tbr Configuration
Register the connection information for the SID referenced in the ODBC configuration in tbdsn.tbr.
fintac1=(
(INSTANCE=(HOST=192.168.51.171)
(PORT=58629)
(DB_NAME=fintac)
)
)
Sample Connection Code
The following odbctest.c is an example code that connects to the Tibero DB using the DSN registered in the .odbc.ini file.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sqlcli.h>
//#include <sqlext.h>
// Global variables
#define SQL_LEN 1000
#define DATA_LEN 100
#define JOB_LEN 9
#define _SUCCESS 0
int main()
{
// Environment variables
SQLRETURN retval;
SQLHENV henv;
SQLHDBC hdbc;
// Allocate ODBC environment memory
retval = SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&henv);
// Set ODBC version (3.0)
retval = SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0);
if(retval == SQL_SUCCESS || retval == SQL_SUCCESS_WITH_INFO)
printf("ODBC environment attribute setting succeeded.\n");
else
printf("ODBC environment attribute setting failed.\n");
// Allocate memory for DB connection
retval = SQLAllocHandle(SQL_HANDLE_DBC,henv,&hdbc);
if(retval == SQL_SUCCESS || retval == SQL_SUCCESS_WITH_INFO)
printf("ODBC connection memory allocation succeeded.\n");
else
printf("ODBC connection memory allocation failed.\n");
// Set connection attributes and connect to DB
SQLSetConnectAttr(hdbc,5,(void*)SQL_LOGIN_TIMEOUT,0);
retval = SQLConnect(hdbc,
(SQLCHAR *)"ODBC_TEST", SQL_NTS,
// DSN info registered in .odbc.ini /
(SQLCHAR *)"tibero", SQL_NTS,
(SQLCHAR *)"tmax", SQL_NTS);
if(retval == SQL_SUCCESS || retval == SQL_SUCCESS_WITH_INFO)
printf("Connection to ODBC database succeeded.\n");
else
printf("Connection to ODBC database failed.\n");
// Disconnect
SQLDisconnect(hdbc);
SQLFreeConnect(hdbc);
SQLFreeEnv(henv);
return 0;
}
Compilation
Use the compilation command appropriate for your operating system environment.
AIX gcc Example
gcc -maix64 -Wl,-brtl -o odbctest odbctest.c -g \
-I$TB_HOME/client/include \
-L$TB_HOME/client/lib -ltbodbc -ltbertl -ltbcli -lpthread -lm
Linux gcc Example
gcc -o odbctest odbctest.c \
-I$TB_HOME/client/include \
-L$TB_HOME/client/lib -ltbodbc -lm -ldlCaution
If you exclude the ltbodbc option, the .odbc.ini file will not be recognized, and the tbdsn.tbr configuration will be recognized first and used.
Some compile and link options may vary depending on the OS, so adjustments according to your environment are necessary.