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

SQL Anywhere 12.0.0 (中文) » UltraLite - C 及 C++ 编程 » API 参考 » 教程:使用 C++ API 构建应用程序

 

第 4 课:将同步添加到应用程序

本课介绍如何同步应用程序和计算机上运行的统一数据库。

以下过程将同步代码添加到应用程序,启动 MobiLink 服务器,然后运行应用程序进行同步。

在前几节课中创建的 UltraLite 数据库与 UltraLite 12 示例数据库同步。UltraLite 12 示例数据库中 ULCustomer 表的列包含本地 UltraLite 数据库中 customer 表的列。

本课假设您熟悉 MobiLink 同步。

 ♦  向应用程序添加同步
  1. 将以下方法添加到 customer.cpp。此方法执行以下任务:

    • 通过调用 EnableTcpipSynchronization 来启用 TCP/IP 通信。同步还可以通过 HTTP、HTTPS 或 TLS 执行。请参见UltraLite 客户端

    • 设置脚本版本。MobiLink 同步由存储在统一数据库中的脚本控制。脚本版本确定要使用的脚本集。

    • 设置 MobiLink 用户名。此值用于 MobiLink 服务器处的验证。它与 UltraLite 数据库用户 ID 不同,尽管在某些应用程序中您可能希望赋予它们相同的值。

    • 将 download_only 参数设置为 true。缺省情况下,MobiLink 同步是双向的。此应用程序使用仅下载同步,因此表中的行不会上载到示例数据库中。



    static bool do_sync( ULConnection * conn )
    {
        ul_sync_info info;
        ul_stream_error * se = &info.stream_error;
      
        ULDatabaseManager::EnableTcpipSynchronization();
        conn->InitSyncInfo( &info );
        info.stream = "TCPIP";
        info.version = "custdb 12.0";
        info.user_name = "50";
        info.download_only = true;
        if( !conn->Synchronize( &info ) ) {
            _tprintf( "Synchronization error \n" ));   
            _tprintf( "  stream_error_code is '%lu'\n", se->stream_error_code );
            _tprintf( "  system_error_code is '%ld'\n", se->system_error_code );
            _tprintf( "  error_string is '" );
            _tprintf( "%s", se->error_string );
            _tprintf( "'\n" );
            return false;
        }
        return true;
    }
  2. 将下行添加到 main 方法中,放在调用 insert 方法的语句和调用 select 方法的语句之间:

    do_sync(conn);
  3. 通过运行 nmake 编译您的应用程序。

 ♦  同步数据
  1. 启动 MobiLink 服务器。

    在命令提示符下,运行以下命令:

    mlsrv12 -c "dsn=SQL Anywhere 12 CustDB;uid=ml_server;pwd=sql" -v -vr -vs -zu+ -o custdbASA.log

    -zu+ 选项提供自动添加用户的功能。-v+ 选项为所有消息启用详细记录。

    有关此选项的详细信息,请参见MobiLink 服务器选项

  2. 通过在命令提示符下键入 customer 运行应用程序。

    MobiLink 服务器消息窗口显示指示同步进度的状态消息。如果同步成功,则最后一条消息显示 [Synchronization complete]。