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

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

 

第 3 课:选择并列出表中的行

以下过程是从表中检索行,并将它们打印到命令行。

♦  列出表中的行
  1. 将以下方法添加到 customer.cpp。此方法执行以下任务:

    • 打开 Table 对象。

    • 检索列标识符。

    • 将当前位置设置在表的第一行之前。

      对表的任何操作都在当前位置执行。此位置可以在表的第一行之前、某一行中或最后一行之后。缺省情况下(如在本例中),行按照其主键值 (cust_id) 进行排序。要按其它方式排序行,可以向 UltraLite 数据库添加索引,然后使用此索引打开表。

    • 对于每一行,将写出 cust_id 和 cust_name 值。循环一直执行到 Next 方法返回 false(检索完最后一行后发生此情况)。

    • 关闭 Table 对象。

    bool do_select( Connection * conn ) {
      Table * table = conn->OpenTable( _TEXT("ULCustomer") );
      if( table == UL_NULL ) {
        return false;
      }
      TableSchema * schema = table->GetSchema();
      if( schema == UL_NULL ) {
        table->Release();
        return false;
      }
      ul_column_num id_cid = 
         schema->GetColumnID( UL_TEXT("cust_id") );
      ul_column_num cname_cid = 
         schema->GetColumnID( UL_TEXT("cust_name") );
      
      schema->Release();
    
      _tprintf( _TEXT("\n\nTable 'ULCustomer' row contents:\n") );
    
      while( table->Next() ) {
        ul_char cname[ MAX_NAME_LEN ];
        
        table->Get( cname_cid ).GetString(
                    cname, MAX_NAME_LEN );
        
        _tprintf( _TEXT("id=%d, name=%s \n"), (int)table->Get(id_cid), cname);
      }
      table->Release();
      return true;
    }
  2. 将下行添加到 main 方法中紧跟对 insert 方法的调用之后的地方:

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

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