![]() | ![]() |
Home |
|
|
|
| Chapter 2: The Embedded SQL Interface |
|
| Application development using Embedded SQL |
|
| Loading the interface library dynamically |
The usual practice for developing applications that use functions from DLLs is to link the application against an import library, which contains the required function definitions.
This section describes an alternative to using an import library for developing Adaptive Server Anywhere applications. The Adaptive Server Anywhere interface library can be loaded dynamically, without having to link against the import library, using the esqldll.c module in the src subdirectory of your installation directory. Using esqldll.c is recommended, because it is easier to use and more robust in its ability to locate the interface DLL.
Your program must call db_init_dll to load the DLL, and must call db_fini_dll to free the DLL. The db_init_dll call must be before any function in the database interface, and no function in the interface can be called after db_fini_dll .
You must still call the db_init and db_fini library functions.
You must #include the esqldll.h header file before the EXEC SQL INCLUDE SQLCA statement or #include <sqlca.h> line in your Embedded SQL program.
A SQL OS macro must be defined. The header file sqlca.h, which is included by esqdll.c, attempts to determine the appropriate macro and defines it. However, certain combinations of platforms and compilers may cause this to fail. In this case, you must add a #define to the top of this file, or make the definition by using a compiler option.
| Macro | Platforms |
| _SQL_OS_WINNT | Windows 95 and Windows NT |
| _SQL_OS_WINDOWS | Windows 3.x |
| _SQL_OS_OS232 | OS/2 |
Compile esqldll.c.
Instead of linking against the imports library, link the object module esqldll.obj with your Embedded SQL application objects.
The following example illustrates how to use esqldll.c and esqldll.h.
#include <stdio.h>
#include "esqldll.h"
EXEC SQL INCLUDE SQLCA;
#include "sqldef.h"
#include <windows.h>
EXEC SQL BEGIN DECLARE SECTION;
int x;
a_sql_statement_number stat1;
EXEC SQL END DECLARE SECTION;
#define TRUE 1
#define FALSE 0
void printSQLError( void )
{
char buffer[200];
sqlerror_message( &sqlca, buffer, sizeof(buffer) );
#ifdef _SQL_OS_WINDOWS
printf( "Error %ld -- %Fs\n", SQLCODE, buffer );
#else
printf( "Error %ld -- %s\n", SQLCODE, buffer );
#endif
}
char *dllpaths[] = { "s:\\jasonhi\\",
NULL };
#include <windows.h>
int main( void )
{
struct sqlda _fd_ * sqlda1;
int result;
char string[200];
printf( "Initing DLL\n" );
result = db_init_dll( dllpaths );
switch( result ) {
case ESQLDLL_OK:
printf("OK\n");
break;
case ESQLDLL_DLL_NOT_FOUND:
printf("DLL NOT FOUND\n");
return( 10 );
case ESQLDLL_WRONG_VERSION:
printf("WRONG VERSION\n");
return( 10 );
}
if( !db_init( &sqlca ) ) {
printf("db_init failed.\n");
db_fini_dll();
return( 10 );
}
#ifdef _SQL_OS_WINNT
result = db_string_connect( &sqlca, "UID=dba;PWD=sql;DBF=d:\\asa6\\sample.db");
#elif defined( _SQL_OS_WINDOWS )
result = db_string_connect( &sqlca, "UID=dba;PWD=sql;DBF=c:\\wsql50\\sample.db");
#elif defined( _SQL_OS_OS232 )
result = db_string_connect( &sqlca, "UID=dba;PWD=sql;DBF=h:\\wsql50\\sample.db");
#endif
if( ! result ) {
printf( "db_string_connect returned = %d\n", result );
printSQLError();
}
sqlda1 = alloc_descriptor( sqlcaptr, 20 );
EXEC SQL PREPARE :stat1 FROM
'select * from employee
WHERE empnum = 80921';
if( SQLCODE != 0 ) {
printSQLError();
}
EXEC SQL DECLARE curs CURSOR FOR :stat1;
if( SQLCODE != 0 ) {
printSQLError();
}
EXEC SQL OPEN curs;
if( SQLCODE != 0 ) {
printSQLError();
}
EXEC SQL DESCRIBE :stat1 INTO sqlda1;
if( SQLCODE != 0 ) {
printSQLError();
}
sqlda1->sqlvar[1].sqltype = 460;
fill_sqlda( sqlda1 );
EXEC SQL FETCH FIRST curs INTO DESCRIPTOR sqlda1;
if( SQLCODE != 0 ) {
printSQLError();
}
printf( "name = %Fs\n", (char _fd_ *)sqlda1->sqlvar[1].sqldata );
x = sqlda1->sqld;
printf( "COUNT = %d\n", x );
free_filled_sqlda( sqlda1 );
db_string_disconnect( &sqlca, "" );
db_fini( &sqlca );
db_fini_dll();
return( 0 );
}|
|