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

SQL Anywhere 12.0.1 » MobiLink - 入门 » MobiLink 教程 » 教程:与 Microsoft Excel 同步

 

第 4 课:添加同步脚本

本课假定您已完成前面的所有课程。 请参见第 1 课:建立 Excel 工作表

在本课中,您将为 SQL 行处理和直接行处理,将脚本添加到统一数据库中。

SQL 行处理允许您将远程数据与 MobiLink 统一数据库中的表同步。基于 SQL 的脚本可定义:

  • 如何将从 MobiLink 客户端上载的数据应用到统一数据库。

  • 应从统一数据库下载哪些数据。

在本课中,您将为以下基于 SQL 的上载和下载事件编写同步脚本:

  • upload_insert   此事件对插入客户端数据库的新订单如何应用到统一数据库进行定义。

  • download_cursor   此事件对应下载到远程客户端的订单进行定义。

  • download_delete_cursor   在使用非仅上载的同步脚本时,需要使用此事件。对于本教程,需将 MobiLink 服务器设置为忽略此事件。

使用直接行处理将特殊处理添加到基于 SQL 的同步系统中。在此过程中,您将注册与 handle_UploadData、handle_DownloadData、download_cursor 和 download_delete_cursor 事件对应的方法名。在下一课中,您将创建自己的 Java 类。

 ♦ 将脚本添加至统一数据库以进行 SQL 行处理和直接行处理
  1. 如果您尚未建立连接,则从 Interactive SQL 连接到统一数据库。

    运行以下命令:

    dbisql -c "DSN=mlexcel_db"
  2. 使用 ml_add_table_script 存储过程为 upload_insert、download_cursor 和 download_delete_cursor 事件添加基于 SQL 的表脚本。

    在 Interactive SQL 中执行以下 SQL 语句。upload_insert 脚本将上载的 order_id、product_id、quantity 和 order_status 插入到 MobiLink 统一数据库。download_cursor 脚本使用基于时间戳的过滤将更新的行下载到远程客户端。



    CALL ml_add_table_script( 'default', 'RemoteOrders', 
        'upload_insert',
        'INSERT INTO RemoteOrders( order_id, product_id, quantity, order_status)
        VALUES( {ml r.order_id}, {ml r.product_id}, {ml r.quantity}, {ml r.order_status} )' );
       
    CALL ml_add_table_script( 'default', 'RemoteOrders',
        'download_cursor',
        'SELECT order_id, product_id, quantity, order_status
        FROM RemoteOrders WHERE last_modified >= {ml s.last_table_download}');
    
    CALL ml_add_table_script( 'default', 'RemoteOrders',
        'download_delete_cursor', '--{ml_ignore}');
    
    COMMIT
  3. 注册用于 handle_UploadData 和 handle_DownloadData 事件的 Java方法。

    在 Interactive SQL 中执行以下 SQL 语句:

    CALL ml_add_java_connection_script( 'default', 
        'handle_UploadData',
        'MobiLinkOrders.GetUpload' );
       
    CALL ml_add_java_connection_script( 'default',
        'handle_DownloadData',
        'MobiLinkOrders.SetDownload' );

    Interactive SQL 分别注册用于 handle_UploadData 和 handle_DownloadData 事件的 GetUpload 和 SetDownload 方法。在后续课程中,将创建这些方法。

  4. 注册 download_cursor 和 download_delete_cursor 事件。

    在 Interactive SQL 中运行以下 SQL 脚本:

    CALL ml_add_table_script( 'default', 'OrderComments',
        'download_cursor', '--{ml_ignore}');
    
    CALL ml_add_table_script( 'default', 'OrderComments',
        'download_delete_cursor', '--{ml_ignore}');

    使用脚本时,download_cursor 和 download_delete_cursor 事件必须进行注册才能用于 OrderComments 表,因为同步是双向同步,而不是仅上载同步。 请参见必需的脚本

  5. 提交所做的更改。

    在 Interactive SQL 中执行以下 SQL 语句:

    COMMIT;
  6. 前进至第 5 课:创建 Java 类进行 MobiLink 直接行处理

 另请参见