只说下碰到的几个问题:
1.unixODBC版本不一致的问题。
oracle选择的是oracle11gR2,与之版本一致的UnixODBC版本是2.2.14,最新的2.3.4是不行的。如果选择2.3.4在用isql测试连接的时候会提示:
[08004][unixODBC][Oracle][ODBC][Ora]ORA-12154: TNS:could not resolve the connect identifier specified
[Isql]ERROR: Could not sqlConnect
2.数据源首先查找系统dsn,找不到再查找用户dsn
3.连接远端oracle服务需要配置正确的tnsname,.odbc.ini中的 ServerName = 【网络服务名tnsname】
4.oracle自带的unixODBC默认查找位置为/etc
5.用unixodbc官网提供的查询示例进行查询测试。gcc query.c –o query –I/$(指定)/incude –L$(指定)/lib –lodbc
6.依赖关系:libodbc.so动态打开驱动 libsqora.so.11.1,驱动libsqora.so.11.1依赖libodbcinst.so
isql依赖libodbc.so,间接依赖驱动libsqora.so.11.1(动态打开的)。
c客户端依赖libodbc.so,间接依赖驱动libsqora.so.11.1(动态打开的)。
7.query.c源码:
#include <stdlib.h> #include <stdio.h> #include <sql.h> #include <sqlext.h> #include <sqltypes.h> sqlHENV V_OD_Env; // Handle ODBC environment long V_OD_erg; // result of functions sqlHDBC V_OD_hdbc; // Handle connection char V_OD_stat[10]; // Status sql sqlINTEGER V_OD_err; sqlLEN V_OD_len; sqlINTEGER V_OD_id; sqlLEN V_OD_rowanz; sqlSMALLINT V_OD_mlen,V_OD_colanz; char V_OD_msg[200],V_OD_buffer[200]; sqlHSTMT V_OD_hstmt; int main(int argc,char *argv[]) { V_OD_erg=sqlAllocHandle(sql_HANDLE_ENV,sql_NULL_HANDLE,&V_OD_Env); if ((V_OD_erg != sql_SUCCESS) && (V_OD_erg != sql_SUCCESS_WITH_INFO)) { printf("Error AllocHandle\n"); exit(0); } V_OD_erg=sqlSetEnvAttr(V_OD_Env,sql_ATTR_ODBC_VERSION,(void*)sql_OV_ODBC3,0); if ((V_OD_erg != sql_SUCCESS) && (V_OD_erg != sql_SUCCESS_WITH_INFO)) { printf("Error SetEnv\n"); sqlFreeHandle(sql_HANDLE_ENV,V_OD_Env); exit(0); } V_OD_erg = sqlAllocHandle(sql_HANDLE_DBC,V_OD_Env,&V_OD_hdbc); if ((V_OD_erg != sql_SUCCESS) && (V_OD_erg != sql_SUCCESS_WITH_INFO)) { printf("Error AllocHDB %d\n",V_OD_erg); sqlFreeHandle(sql_HANDLE_ENV,V_OD_Env); exit(0); } sqlSetConnectAttr(V_OD_hdbc,sql_LOGIN_TIMEOUT,(sqlPOINTER *)5,0); V_OD_erg = sqlConnect(V_OD_hdbc,(sqlCHAR*) "zyx",sql_NTS,(sqlCHAR*) "cbadevdb",sql_NTS); if ((V_OD_erg != sql_SUCCESS) && (V_OD_erg != sql_SUCCESS_WITH_INFO)) { printf("Error sqlConnect %d\n",V_OD_erg); sqlGetDiagRec(sql_HANDLE_DBC,V_OD_hdbc,1,V_OD_stat,&V_OD_err,V_OD_msg,100,&V_OD_mlen); printf("%s (%d)\n",V_OD_err); sqlFreeHandle(sql_HANDLE_ENV,V_OD_Env); exit(0); } printf("Connected !\n"); V_OD_erg=sqlAllocHandle(sql_HANDLE_STMT,&V_OD_hstmt); if ((V_OD_erg != sql_SUCCESS) && (V_OD_erg != sql_SUCCESS_WITH_INFO)) { printf("Fehler im AllocStatement %d\n",V_OD_err); sqlDisconnect(V_OD_hdbc); sqlFreeHandle(sql_HANDLE_DBC,V_OD_hdbc); sqlFreeHandle(sql_HANDLE_ENV,V_OD_Env); exit(0); } sqlBindCol(V_OD_hstmt,sql_C_CHAR,&V_OD_buffer,150,&V_OD_len); sqlBindCol(V_OD_hstmt,2,sql_C_ULONG,&V_OD_id,&V_OD_len); V_OD_erg=sqlExecDirect(V_OD_hstmt,"SELECT name,id FROM stu order by id",sql_NTS); if ((V_OD_erg != sql_SUCCESS) && (V_OD_erg != sql_SUCCESS_WITH_INFO)) { printf("Error in Select %d\n",V_OD_erg); sqlGetDiagRec(sql_HANDLE_DBC,&V_OD_mlen); printf("%s (%d)\n",V_OD_err); sqlFreeHandle(sql_HANDLE_STMT,V_OD_hstmt); sqlDisconnect(V_OD_hdbc); sqlFreeHandle(sql_HANDLE_DBC,V_OD_hdbc); sqlFreeHandle(sql_HANDLE_ENV,V_OD_Env); exit(0); } V_OD_erg=sqlNumResultCols(V_OD_hstmt,&V_OD_colanz); if ((V_OD_erg != sql_SUCCESS) && (V_OD_erg != sql_SUCCESS_WITH_INFO)) { sqlFreeHandle(sql_HANDLE_STMT,V_OD_hstmt); sqlDisconnect(V_OD_hdbc); sqlFreeHandle(sql_HANDLE_DBC,V_OD_hdbc); sqlFreeHandle(sql_HANDLE_ENV,V_OD_Env); exit(0); } printf("Number of Columns %d\n",V_OD_colanz); V_OD_erg=sqlRowCount(V_OD_hstmt,&V_OD_rowanz); if ((V_OD_erg != sql_SUCCESS) && (V_OD_erg != sql_SUCCESS_WITH_INFO)) { printf("Number ofRowCount %d\n",V_OD_erg); sqlFreeHandle(sql_HANDLE_STMT,V_OD_hstmt); sqlDisconnect(V_OD_hdbc); sqlFreeHandle(sql_HANDLE_DBC,V_OD_hdbc); sqlFreeHandle(sql_HANDLE_ENV,V_OD_Env); exit(0); } printf("Number of Rows %d,%d\n",V_OD_rowanz,V_OD_erg); V_OD_erg=sqlFetch(V_OD_hstmt); while(V_OD_erg != sql_NO_DATA) { printf("Result: %d %s\n",V_OD_id,V_OD_buffer); V_OD_erg=sqlFetch(V_OD_hstmt); } ; sqlFreeHandle(sql_HANDLE_STMT,V_OD_hstmt); sqlDisconnect(V_OD_hdbc); sqlFreeHandle(sql_HANDLE_DBC,V_OD_hdbc); sqlFreeHandle(sql_HANDLE_ENV,V_OD_Env); return(0); }原文链接:https://www.f2er.com/oracle/211375.html