Contents IndexLibrary functions Embedded SQL commands

User's Guide
   Part V. The SQL Anywhere Programming Interfaces
     Chapter 34. The Embedded SQL Interface
      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 SQL Anywhere applications. The SQL 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 as it is easier to use and more robust in its ability to locate the interface DLL.

To use the dynamic interface library loading:

  1. Your program must call db_init_dllto load the DLL, and must call db_fini_dllto 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_initand db_finilibrary functions.

  2. 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.
  3. You must ensure that a SQL OS macro is defined when compiling ESQLDLL.C. The header file SQLCA.H, which is included by this file, 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 32-bit OS/2

  4. Compile ESQLDLL.C.
  5. Instead of linking against the imports library, link the object module ESQLDLL.OBJ with your Embedded SQL application objects.

Example

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:\\sqlany50\\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 );
     }

Contents IndexLibrary functions Embedded SQL commands