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 » Developing Applications Using the UltraLite C++ API

Using the UltraLite namespace Next Page

Connecting to a database

UltraLite applications must connect to the database before performing operations on its data. This section describes how to connect to an UltraLite database.

You can find sample code in the samples-dir\UltraLite\CustDB\ directory.

Properties of the Connection object
Connecting to an UltraLite database
To connect to an UltraLite database
  1. Use the UltraLite namespace.

    Using the UltraLite namespace allows you to use simple names for classes in the C++ interface.

    using namespace UltraLite;
  2. Create and initialize a DatabaseManager object and an UltraLite SQL communications area (ULSqlca). The ULSqlca is a structure that handles communication between the application and the database.

    The DatabaseManager object is at the root of the object hierarchy. You create only one DatabaseManager object per application. It is often best to declare the DatabaseManager object as global to the application.

    ULSqlca sqlca;
    DatabaseManager * dbMgr = ULInitDatabaseManager(sqlca);

    If the application does not require SQL support and directly links the UltraLite runtime, the application can call ULInitDatabaseManagerNoSQL to initialize the ULSqlca. This variant reduces the size of the application.

    See UltraLite_DatabaseManager_iface class.

  3. Open a connection to an existing database, or create a new database if the specified database file does not exist. See OpenConnection function.

    UltraLite applications can be deployed with an initial empty database or the application can create the UltraLite database if it does not already exist. Deploying an initial database is the simplest solution; otherwise, the application must call the ULCreateDatabase function to create the database and must create all the tables the application requires. See ULCreateDatabase function.

    COnnection * conn = dbMgr->OpenConnection( sqlca, UL_TEXT("dbf=mydb.udb") );
    if( sqlca.GetSQLCode() == 
        printf( "Open failed with sql code: %d.\n" , sqlca.GetSQLCode() );
Multi-threaded applications

Each connection and all objects created from it should be used by a single thread. If an application requires multiple threads accessing the UltraLite database, each thread requires a separate connection.