Lesson 2: Add synchronization scripts

You can view, write, and modify synchronization scripts using Sybase Central. In this section you write the following synchronization scripts:

  • upload_insert   This event defines how new client-side data should be applied to the consolidated database.

  • download_cursor   This event defines the data that should be downloaded to remote clients.

  • download_delete_cursor   This event is required when using synchronization scripts that are not upload-only. You set the MobiLink server to ignore this event for the purpose of this tutorial.

Connect to the consolidated database by creating a new MobiLink project.

 To create a new MobiLink project
  1. Choose Start » Programs » SQL Anywhere 12 » Administration Tools » Sybase Central.

  2. From the Tools menu, choose MobiLink 12 » New Project.

  3. In the Name field, type mlmon_project.

  4. In the Location field, type C:\mlmon, and then click Next.

  5. Check the Add A Consolidated Database To The Project option.

  6. In the Database Display Name field, type mlmon_db.

  7. Click Edit. The Connect To A Generic ODBC Database window appears.

  8. In the User ID field, type DBA.

  9. In the Password field, type sql.

  10. In the ODBC Data Source name field, click Browse and select mlmon_db.

  11. Click OK, then click Save.

  12. Check the Remember The Password option, and then click Finish.

  13. Click OK.

Each script belongs to a designated script version. You must add a script version to the consolidated database before you add scripts.

 To add a script version
  1. From the View menu, choose Folders.

  2. In the left pane of Sybase Central under MobiLink 12, expand mlmon_project, Consolidated Databases and then mlmon_db - DBA.

  3. Right-click Versions and choose New » Version.

  4. In the What Do You Want To Name The New Script Version field, type ver1.

  5. Click Finish.

You must register the Product table as a synchronization table before you can add scripts to that table.

 To add synchronized tables to your consolidated database
  1. From the View menu, choose Folders.

  2. In the left pane of Sybase Central under MobiLink 12, expand mlmon_project, Consolidated Databases and then mlmon_db - DBA.

  3. Right-click Synchronized Tables and choose New » Synchronized Table.

  4. Click the Choose A Table In The Consolidated Database With The Same Name As The Remote Table option.

  5. In the Which User Owns The Table You Want To Synchronize list, click DBA.

  6. In the Which Table Do You Want To Synchronize list, click Product.

  7. Click Finish.

Add a new table script for each upload and download to the consolidated database.

 To add table scripts for the Product table
  1. From the View menu, choose Folders.

  2. In the left pane of Sybase Central under MobiLink 12, expand mlmon_project, Consolidated Databases, mlmon_db and then Synchronized Tables.

  3. Right-click Product and choose New » Table Script.

  4. In the For Which Version Do You Want To Create The Table Script list, click ver1.

  5. In the Which Event Should Cause The Table Script To Be Executed list, click upload_insert, and then click Next.

  6. Click Finish.

  7. In the right pane of Sybase Central, use the following SQL script for the upload_insert event:

    INSERT INTO Product( name, quantity, last_modified )
        VALUES( {ml r.name}, {ml r.quantity}, now() )

    The upload_insert event determines how data inserted into the remote database should be applied to the consolidated database. For more information about upload_insert, see upload_insert table event.

  8. Choose File » Save.

  9. Repeat steps 2 to 5, specifying the download_cursor event instead of the upload_insert event in step 4.

  10. In the right pane of Sybase Central, use the following SQL script for the download_cursor event:

    SELECT name, quantity FROM Product
        WHERE last_modified >= {ml s.last_table_download}

    The download_cursor script defines a cursor to select consolidated database rows that are downloaded and inserted or updated in the remote database. For more information about download_cursor, see download_cursor table event.

  11. Choose File » Save.

  12. Repeat steps 2 to 5, specifying the download_delete_cursor event instead of the upload_insert event in step 4.

  13. In the right pane of Sybase Central, use the following SQL script for the download_delete_cursor event:

    --{ml_ignore}
  14. Choose File » Save.

 Further reading