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 Embedded SQL

Lesson 2: Configure eMbedded Visual C++ Next Page

Lesson 3: Write an embedded SQL source file

The following procedure creates a tutorial program that establishes a connection with the UltraLite CustDB sample database and executes a query.

To build the tutorial Embedded SQL UltraLite application
  1. Start Microsoft eMbedded Visual C++.

    From the Start menu, choose All Programs > Microsoft eMbedded Visual C++ 4.0 > eMbedded Visual C++ 4.0

  2. In eMbedded Visual C++ create a new workspace named Ultralite:

    1. Select File > New.

    2. Click the Workspaces tab.

    3. Choose Blank Workspace. Specify a workspace name Ultralite and specify C:\tutorial as the location to save this workspace. Click OK.

      The Ultralite workspace is added to the Workspace window.

  3. Create a new project named esql and add it to the Ultralite workspace.

    1. Select File > New.

    2. Click the Projects tab.

    3. The application types available depend on the SDKs you have installed. Choose an application type appropriate for the device you are using. For example, if you are using a Pocket PC 2002 device, choose WCE Pocket PC 2002 Application. Specify a project name esql and select Add To Current Workspace. Select the applicable CPUs. Click OK.

    4. Choose Create An Empty Project and click Finish.

      The project is saved in the c:\tutorial\esql folder.

  4. Create the sample.sqc source file.

    1. Choose File > New.

    2. Click the Files tab.

    3. Select C++ Source File.

    4. Select Add to Project and choose esql from the dropdown list.

    5. Name the file sample.sqc. Click OK.

    6. Copy the following source code into the file. The next section describes this tutorial code in detail.

      #include <tchar.h>
      #include <windows.h>
      int WINAPI WinMain( HINSTANCE hInstance, 
          HINSTANCE hPrevInstance, 
          LPTSTR lpCmdLine, 
          int nShowCmd)
         /* Declare fields */
            long pid=1;
            long cost;
            TCHAR pname[31];
         TCHAR  output[200];
         TCHAR  result[10];
         /* Before working with data*/
         /* Connect to database */
         EXEC SQL CONNECT USING 'dbf=\Windows\Start Menu\esqldb.udb';  
         /* Fill table with data first */
         EXEC SQL INSERT INTO ULProduct(
               prod_id, price, prod_name)
            VALUES (1, 400, '4x8 Drywall x100');
         EXEC SQL INSERT INTO ULProduct (
               prod_id, price, prod_name)
            VALUES (2, 3000, '8''2x4 Studs x1000');
         /* Commit changes (INSERTs) to database */
         EXEC SQL COMMIT;   
         /* Fetch row 1 from database */
         EXEC SQL SELECT price, prod_name
               INTO :cost, :pname
               FROM ULProduct
               WHERE prod_id= :pid; 
               /* pid was initialized to 1, so get first row inserted above */
         /* Print query results */
         wsprintf( output
                  , TEXT("Product id: %d\n     price: %d\n      name: %s")
                  , pid , cost , pname );
         MessageBox(NULL, output, result, MB_OK);
         /* Preparing to exit: disconnect */
         /* Error handling.  If the row does not exist,
            or if an error occurs, -1 is returned */
          if((SQLCODE==SQLE_NOTFOUND)||(SQLCODE<0)) {
              wsprintf (output, TEXT("error: %d"), SQLCODE );
              MessageBox(NULL, output, result, MB_OK);
    7. Save the file.

  5. Configure the sample.sqc source file settings to invoke the SQL preprocessor to preprocess the source file:

    1. Right-click sample.sqc in the Workspace window and choose Settings.

      The Project Settings dialog appears.

    2. From the Settings For dropdown list, choose All Configurations.

    3. In the Custom Build tab, enter the following statement in the Commands field. Ensure that the statement is entered all on one line.

      "%SQLANY10%\win32\sqlpp.exe" -q -u $(InputPath) sample.cpp

      This statement runs the SQL preprocessor sqlpp on the sample.sqc file, and writes the processed output to a file named sample.cpp. The SQL preprocessor translates SQL statements in the source file into C/C++ function calls.

      For more information about the SQL preprocessor, see SQL preprocessor.

    4. Type sample.cpp in the Outputs field.

    5. Click OK to submit the changes.

  6. Preprocess the sample.sqc file.

    1. Select sample.sqc in the Workspace window. Choose Build > Compile sample.sqc. A sample.cpp file is created and saved in the tutorial\esql directory.

  7. Add sample.cpp (the output of the SQL preprocessor) to the project:

    1. Right-click the Source Files folder in the Workspace window and choose Add Files to Folder.

    2. Browse to c:\tutorial\esql\sample.cpp and click OK.

      The sample.cpp file now appears inside the Source Files folder.

Explanation of the tutorial program