| Oracle FAQ | Your Portal to the Oracle Knowledge Grid | |
Home -> Community -> Usenet -> c.d.o.misc -> Re: Need ODBC Example Code
What follows is the LCC32 version of my ODBC C program to show
how to use cursors on an Oracle 8 DB. The LCC32 version of the
program (.EXE) was just 17KB rather than the 100KB size of the
vc++ 5 program (both had debug data set to ON).
LCC32 is a very usable 32 bit C/C++ FREEWARE compiler. You can get it at:
http://www.qss.cz/lcc/ http://www.cs.virginia.edu/~lcc-win32/ http://www.delorie.com/
Code follows:
// -------------------------------------------------------------- ---------------------- // Program: odbc // Purpose: C program to demo ODBC on an Oracle 8 database table // by: Rock Cogar, Radian International LLC, OR TN // date: January 27, 2000 // Compiler: LCC32 // Environment: Windows NT 4.0 on Pentium III // Style: Win32 Console application // DBMS: Assumes Schema SCOTT (password ="tiger") is present with table emp // ODBC: Assumes ODBC alias called "oracle" points tothe oracle DBMS
// -------------------------------------------------------------- ----------------------#include <sql.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <direct.h>
#include <conio.h>
#include <io.h>
#define SZLEN 16
#define SZDATELEN 24
void process(void);
void dbError( LPSTR lp, HENV henv,HDBC hdbc,HSTMT hstmt);
// -------------------------------------------------------------- ----------------------------------------------void main(void)
// -------------------------------------------------------------- ---------------------------------------------- // -------------------------------------------------------------- ----------------------------------------------void process(void)
HENV henv; HDBC hdbc; char szSql[256]; char szout[256];
retcode = SQLAllocEnv(&henv); /* Environment handle */
if (retcode != SQL_SUCCESS)
{
dbError( "SQLAllocEnv()",henv,hdbc,hstmt);
return;
}
retcode = SQLAllocConnect(henv, &hdbc); /* Connection handle */
if (retcode != SQL_SUCCESS)
{
dbError( "SQLAllocConnect()",henv,hdbc,hstmt);
SQLFreeEnv(henv);
return;
}
SQLSetConnectOption(hdbc, 103, 15); /* Set login timeout to 15
seconds. */
retcode = SQLConnect(hdbc, "oracle", SQL_NTS, "scott",
SQL_NTS, "tiger", SQL_NTS); /* Connect to data source */
if (retcode != SQL_SUCCESS && retcode != SQL_SUCCESS_WITH_INFO)
{
dbError( "SQLConnect()",henv,hdbc,hstmt);
SQLFreeEnv(henv);
return;
}
retcode = SQLAllocStmt(hdbc, &hstmt); /* Statement handle */
if (retcode != SQL_SUCCESS)
{
dbError( "SQLAllocStmt()",henv,hdbc,hstmt);
SQLFreeEnv(henv);
return;
}
lstrcpy( szSql,"select empno,ename,job,mgr,hiredate,sal,nvl (comm,0),deptno from emp order by empno asc");
retcode = SQLExecDirect(hstmt, szSql, SQL_NTS);
if (retcode != SQL_SUCCESS)
{
dbError( " SQLExecDirect()",henv,hdbc,hstmt);
}
if (retcode == SQL_SUCCESS)
{
while (TRUE)
{
retcode = SQLFetch(hstmt);
if (retcode == SQL_ERROR || retcode ==
SQL_SUCCESS_WITH_INFO)
{
dbError( "SQLFetch()",henv,hdbc,hstmt);
}
if (retcode == SQL_SUCCESS || retcode ==
SQL_SUCCESS_WITH_INFO)
{
SQLGetData(hstmt, 1,SQL_DOUBLE,
&dblempno, 0, &cbempno);
SQLGetData(hstmt, 2,SQL_CHAR,
szename, SZLEN, &cbename);
SQLGetData(hstmt, 3,SQL_CHAR,
szjob, SZLEN, &cbjob);
SQLGetData(hstmt, 4,SQL_DOUBLE,
&dblmgr, 0, &cbmgr);
SQLGetData(hstmt, 5,SQL_DATETIME,
&ts, 16, &cbhiredate);
SQLGetData(hstmt, 6,SQL_DOUBLE,
&dblsal, 0, &cbsal);
SQLGetData(hstmt, 7,SQL_DOUBLE,
&dblcomm, 0, &cbcomm);
SQLGetData(hstmt, 8,SQL_DOUBLE,
&dbldeptno, 0, &cbdeptno);
sprintf( szhiredate,"%d-%d-%
d",ts.month,ts.day,ts.year);
sprintf(szout,"%6.0f\t%s\t%s\t%6.0f\t%
s\t%6.0f\t%6.0f\t%
puts(szout);
}
else
{
break;
}
}
}
SQLFreeStmt(hstmt, SQL_DROP);
SQLDisconnect(hdbc);
SQLFreeConnect(hdbc);
SQLFreeEnv(henv);
}
// -------------------------------------------------------------- ---------------------------------------------- // -------------------------------------------------------------- ----------------------------------------------void dbError( LPSTR lp, HENV henv,HDBC hdbc,HSTMT hstmt) {
SQLError( henv, hdbc, hstmt, sqlstate, NULL,buf, sizeof(buf),
NULL);
fprintf(stderr, "%s. %s, SQLSTATE=%s\n",lp, buf, sqlstate);
}
// -------------------------------------------------------------- -----------------------------------------------
![]() |
![]() |