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

SAP Sybase SQL Anywhere 16.0 » UltraLite - C and C++ Programming » Application development » UltraLite C++ application development » UltraLite database connections

 

Connecting to an UltraLite database

Use the ULDatabaseManager object to create or connect to an UltraLite database named sample.udb.

Prérequis

There are no prerequisites for this task.

 Task
  1. Initialize the ULDatabaseManager object and enable features in UltraLite using the following code:

    if( !ULDatabaseManager::Init() ) {
        return 0;
    }
    ULDatabaseManager::EnableAesDBEncryption();
     
    // Use ULDatabaseManager.Fini() when terminating the app.
  2. Open a connection to an existing database or create a new database if the specified database file does not exist using the following code:



    ULConnection * conn;
    ULError ulerr;
     
    conn = ULDatabaseManager::OpenConnection( "dbf=sample.udb;dbkey=aBcD1234", &ulerr );
    if( conn == NULL ) {
        if( ulerr.GetSQLCode() == SQLE_ULTRALITE_DATABASE_NOT_FOUND ) {
            conn = ULDatabaseManager::CreateDatabase( "dbf=sample.udb;dbkey=aBcD1234", &ulerr );
            if( conn == NULL ) {
                // write code that uses ulerr to determine what happened
                return 0;
            }
            // add code to create the schema for your database
        } else {
            // write code that uses ulerr to determine what happened
            return 0;
        }
    }
    assert( conn != NULL );

    In this step, you declare a ULError object that contains error information in case the connection is not successful.

Résultat

A connection to the sample.udb database is established.