Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 10.0.1 » UltraLite - C and C++ Programming » Tutorial: Build an Application Using ODBC

Lesson 2: Create an UltraLite database Next Page

Lesson 3: Connect to the database


UltraLite uses standard ODBC programming methods to connect to a database. Each application requires an environment handle to manage the communication with UltraLite and a connection handle for a specific connection.

Write code to allocate an environment handle
  1. Add the opendb and closedb functions to sample.cpp:

    static SQLHANDLE opendb( void ){
        SQLRETURN retn;
        SQLHANDLE henv;
        retn = SQLAllocHandle( SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv );
        if( retn == SQL_SUCCESS ){
            _tprintf( "success in opendb: %d.\n", retn );
            return henv;
        } else {
            _tprintf( "error in opendb: %d.\n", retn );
            return henv;
        }
    }
    
    static void closedb( SQLHANDLE henv ){
        SQLRETURN retn;
        retn = SQLFreeHandle( SQL_HANDLE_ENV, henv );
    }

    These functions do not connect to the database, they simply allocate the environment handle henv that manages UltraLite features.

  2. Call opendb and closedb from the main function.

    Alter your main function in sample.cpp so that it reads as follows:

    int main() {
        SQLHANDLE henv;
        henv = opendb();
        closedb( henv );
        return 0;
    }
  3. Compile, link, and run your application to confirm that the application builds properly.

For more information about the functions called in this procedure, see SQLAllocHandle function, and SQLFreeHandle function.

The next step is to connect to the UltraLite database

Write code to connect to your database
  1. Add connect and disconnect functions to sample.cpp:

    static SQLHANDLE connect ( SQLHANDLE henv ){
        SQLRETURN retn;
        SQLHANDLE hcon;
        retn = SQLAllocHandle( SQL_HANDLE_DBC, henv, &hcon );
        retn = SQLConnect( hcon
         , (SQLTCHAR*)UL_TEXT( 
             "dbf=customer.udb" )
         , SQL_NTS
         , (SQLTCHAR*)UL_TEXT( "DBA" )
         , SQL_NTS
         , (SQLTCHAR*)UL_TEXT( "sql" )
         , SQL_NTS );
        if( retn == SQL_SUCCESS ){
            _tprintf( "success in connect: %d.\n", retn );
            return hcon;
        } else {
            _tprintf( "error in connect: %d.\n", retn );
            return hcon;
        }
    }
    
    static void disconnect( SQLHANDLE hcon, SQLHANDLE henv ){
        SQLRETURN retn;
        retn = SQLDisconnect( hcon );
        retn = SQLFreeHandle( SQL_HANDLE_DBC, hcon );
    }
  2. Call connect and disconnect from the main function.

    Alter your main function in sample.cpp so that it reads as follows:

    int main() {
        SQLHANDLE henv;
        SQLHANDLE hcon;
        henv = opendb();
        hcon = connect( henv );
        disconnect( hcon, henv );
        closedb( henv );
        return 0;
    }
  3. Compile, link, and run your application to confirm that the application builds properly.

For more information about the functions called in this procedure, see SQLConnect function, and SQLDisconnect function.

You now have an application that connects to and disconnects from a database. The next step is to add some data to the database.