Chinaunix首页 | 论坛 | 博客
  • 博客访问: 836781
  • 博文数量: 109
  • 博客积分: 650
  • 博客等级: 上士
  • 技术积分: 1483
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-01 17:29
文章分类

全部博文(109)

文章存档

2016年(5)

2015年(21)

2014年(16)

2013年(38)

2012年(29)

分类: Oracle

2013-03-16 09:59:36

Thank you, of course I searched in the WWW
( With google, which  also finds this pages )
But the tip for looking in the of manual-pages at 
was usefull.

I had to do some changes, but now it works.
Maybe I would help some others trying the same.
( I used unixODBC-devel-2.2.2-73 , included in SuSE 8.1 )

in the odbc.c changed the includes:
from
#include 
#include 
#include 
to
#include 
#include 
#include 

and the connect:

V_OD_erg = SQLConnect(V_OD_hdbc, (SQLCHAR*) "SAPDB_INTERNAL", SQL_NTS,
                                      (SQLCHAR*) "MYUSER", SQL_NTS,
                                      (SQLCHAR*) "MYPWD", SQL_NTS);

Comiling and linking:

gcc odbc.c -o odbc.o -lodbc
ld -o odbc odbc.o -L/usr/lib

in /etc/unixODBC.ini I have

[SAPDB_INTERNAL]
Description     = SapDB Internal-Mode
Driver          = /opt/sapdb/interfaces/odbc/lib/libsqlod.so
ServerType      = odbc
ServerNode      = myServer
ServerDB        = MYSAPDB
SQLMode         = INTERNAL
IsolationLevel  = Committed
TraceFileName   = /var/log/sapdb/ODBCmySAPdbInternal.log


-------- Original-Nachricht --------
Betreff: Re: Example for C using ODBC for Linux ?
Datum: Tue, 22 Jul 2003 13:53:32 -0500
Von: Derek Colley
An: linux.general at listserv.sap.com Referenzen: <3F1D267C.8000200 at webde-ag.de>

Have you tried searching for resources on Yahoo?  brings up a 
list of
related sites.

Most relevant is the http:// website. Check the Manuals 
section
for a programmer's guide.

--
Derek


Quoting mkl at webde-ag.de:

> I`m searching for an C / C++ source > to connect to sapdb via ODBC > on Linux. > > _______________________________________________ > linux.general mailing list > linux.general at listserv.sap.com >  >  ------------------------------------------------------------
webMail supplied & supported by  _______________________________________________
linux.general mailing list linux.general at listserv.sap.com 


	
	
		
		
... SQLHSTMT hstmt=SQL_NULL_HSTMT; //定义语句句柄 SQLHSTMT(sql handle statement) SQLHENV henv; //定义环境句柄 SQLHENV(sql handle environment) SQLHDBC hdbc;//定义连接句柄 SQLHDBC(sql handle database Connection) SQLRETURN retcode; SQLSMALLINT ConnStrOutLen; SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&henv); //分配odbc环境句柄 ...
在vc++ 6.0里,用c++通过ODBC API,使用函数SQLConnect连接数据库。 #include #include #include "sqlext.h" using namespace std; /*用ODBC API访问数据库:连接数据库*/ int main(){ SQLHENV henv; //环境句柄 SQLHDBC hdbc; //连接句柄 SQLHSTMT hstmt; //语句句柄 SQLRETURN retcode; //返回值 /*分配环境句柄*/ retcode = SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&henv); if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO){ /*设置ODBC环境属性*/ retcode = SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0); if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO){ /*分配连接句柄*/ retcode = SQLAllocHandle(SQL_HANDLE_DBC,henv,&hdbc); if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO){ /*连接到数据源*/ retcode = SQLConnect (hdbc, (SQLCHAR*)"Sales",SQL_NTS,(SQLCHAR*)"",SQL_NTS, (SQLCHAR*)"",SQL_NTS); if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO){ /*分配语句句柄*/ retcode = SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt); if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO){ /*处理数据*/ cout << "ok" << endl; SQLFreeHandle(SQL_HANDLE_STMT,hstmt); } SQLDisconnect(hdbc); } SQLFreeHandle(SQL_HANDLE_DBC,hdbc); } } } return 0; } 其中,Sales为数据源,因为数据源没有用户名和密码,所以,紧接着两个参数都为“”。 在vc++ 6.0里,用c++通过ODBC API,使用函数SQLConnect连接数据库。 #include #include #include "sqlext.h" using namespace std; /*用ODBC API访问数据库:连接数据库*/ int main(){ SQLHENV henv; //环境句柄 SQLHDBC hdbc; //连接句柄 SQLHSTMT hstmt; //语句句柄 SQLRETURN retcode; //返回值 /*分配环境句柄*/ retcode = SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&henv); if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO){ /*设置ODBC环境属性*/ retcode = SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0); if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO){ /*分配连接句柄*/ retcode = SQLAllocHandle(SQL_HANDLE_DBC,henv,&hdbc); if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO){ /*连接到数据源*/ retcode = SQLConnect (hdbc, (SQLCHAR*)"Sales",SQL_NTS,(SQLCHAR*)"",SQL_NTS, (SQLCHAR*)"",SQL_NTS); if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { /*分配语句句柄*/ retcode = SQLAllocHandle(SQL_HANDLE_STMT,hdbc,&hstmt); if(retcode == SQL_SUCCESS || retcode == SQL_SUCCESS_WITH_INFO) { /*处理数据*/ cout << "ok" << endl; SQLFreeHandle(SQL_HANDLE_STMT,hstmt); } SQLDisconnect(hdbc); } SQLFreeHandle(SQL_HANDLE_DBC,hdbc); } } } return 0; } 其中,Sales为数据源,因为数据源没有用户名和密码,所以,紧接着两个参数都为“”。
为了完成这学期的数据库课程设计。于是先做了一下使用odbc连接mysql数据库的实验,代码如下: //我连接的是用户dsn,数据源的名字是market #include #include #include #include #include #include #include #include #include #include using namespace std; class SqlSearch { public: void SetDatabase(WCHAR* const name) {lstrcpy(database,name);} void SetUser( WCHAR* const tmpu,WCHAR* const tmpp) {lstrcpy(user,tmpu);lstrcpy(psw,tmpp);} void DisConnect() {SQLFreeHandle(SQL_HANDLE_DBC,hconn);} bool Connect() { short tmp; tmp=SQLAllocHandle(SQL_HANDLE_ENV,SQL_NULL_HANDLE,&henv); if(tmp!=SQL_SUCCESS&&tmp!=SQL_SUCCESS_WITH_INFO) return false; tmp=SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0); if(tmp!=SQL_SUCCESS&&tmp!=SQL_SUCCESS_WITH_INFO) return false; tmp=SQLAllocHandle(SQL_HANDLE_DBC,henv,&hconn); if(tmp!=SQL_SUCCESS&&tmp!=SQL_SUCCESS_WITH_INFO) return false; tmp=SQLConnect(hconn,database,lstrlen(database),user,lstrlen(user),psw,lstrlen(psw)); if(tmp!=SQL_SUCCESS&&tmp!=SQL_SUCCESS_WITH_INFO) return false; return true; } bool ExcuteSqldml(WCHAR* sql,HANDLE& hstmt) { bool flag=true; short tmp; do{ tmp=SQLAllocHandle(SQL_HANDLE_STMT,hconn,&hstmt); if(tmp!=SQL_SUCCESS&&tmp!=SQL_SUCCESS_WITH_INFO) {return false;} tmp=SQLSetStmtAttr(hstmt,SQL_ATTR_CURSOR_TYPE,(void*)SQL_CURSOR_STATIC,0); if(tmp!=SQL_SUCCESS&&tmp!=SQL_SUCCESS_WITH_INFO) {flag=false;break;} int len=lstrlen(sql); tmp=SQLExecDirect(hstmt,sql,len); if(tmp!=SQL_SUCCESS&&tmp!=SQL_SUCCESS_WITH_INFO) {flag=false;break;} }while(0); return flag; } protected: HANDLE henv; HANDLE hconn; WCHAR database[100];//数据库名 WCHAR user[100];//用户名 WCHAR psw[100];//密码 }; WCHAR * sql=L"select * from goods"; struct Result { char id[10]; char name[10]; char price[10]; }; int main() { SqlSearch wss; wss.SetDatabase(L"market"); wss.SetUser(L"root",L"jy03192178"); if(!wss.Connect()) return 0; HANDLE hstmt; wss.ExcuteSqldml(sql,hstmt); Result result; SQLBindCol(hstmt,1,SQL_C_CHAR,&result.id[0],sizeof(result.id),NULL); SQLBindCol(hstmt,2,SQL_C_CHAR,&result.name[0],sizeof(result.id),NULL); SQLBindCol(hstmt,3,SQL_C_CHAR,&result.price[0],sizeof(result.id),NULL); while(SQLFetch(hstmt)!=SQL_NO_DATA) cout<




阅读(2555) | 评论(0) | 转发(0) |
0

上一篇:apue.h

下一篇:C++ 学习之路

给主人留下些什么吧!~~