UltraLite Static C++ User's Guide
Tutorial: Build an Application Using the C++ API
UltraLite applications exchange data with a consolidated database. In this lesson, you add synchronization to the simple application you created in the previous section. In addition, you change the output to verify that synchronization has taken place.
Adding synchronization actually simplifies the code. Your initial version of main.cpp has the following lines, that insert data into your UltraLite database.
productTable.SetProd_id( 1 ); productTable.SetPrice( 400 ); productTable.SetProd_name( "4x8 Drywall x100" ); productTable.Insert(); productTable.SetProd_id( 2 ); productTable.SetPrice( 3000 ); productTable.SetProd_name( "8' 2x4 Studs x1000" ); productTable.Insert();
This code is included to provide an initial set of data for your application. In a production application, you would usually not insert an initial copy of your data from source code, but instead carry out a synchronization.
To add synchronization to your application
Add a synchronization information structure to your code.
Add the following line immediately after the line that says // (2) declare variables
.
auto ul_synch_info synch_info;
This structure holds the parameters that control the synchronization.
Replace the explicit inserts with a synchronization call.
Delete the productTable methods listed above.
Add the following lines in their place:
conn.InitSynchInfo( &synch_info ); synch_info.user_name = UL_TEXT( "50" ); synch_info.version = UL_TEXT( "custdb 9.0" ); synch_info.stream = ULSocketStream(); synch_info.stream_parms = UL_TEXT("host=localhost"); conn.Synchronize( &synch_info );
The value of 50
is the MobiLink user name.
The string custdb
instructs MobiLink to use the default script version for synchronization.
ULSocketStream()
instructs the application to synchronize over TCP/IP, and host=localhost
specifies the host name of the MobiLink server, which in this case is the current machine.
Compile and link your application.
Select Build
Start the MobiLink server running against the sample database.
From a command prompt in your APITutorial directory, enter the following command:
start dbmlsrv9 -c "dsn=UltraLite 9.0 Sample"
Run your application.
From the Build menu, choose Execute APITutorial.exe.
The application connects, synchronizes to receive data, and writes out information to the command prompt window. The output is as follows:
The ULData object is open Price: 400 Price: 3000 Price: 40 Price: 75 Price: 100 Price: 400 Price: 3000 Price: 75 Price: 40 Price: 100