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 - M-Business Anywhere 编程 » 了解 UltraLite for M-Business Anywhere 开发 » 使用 SQL 处理数据

 

数据检索:SELECT

执行 SELECT 语句时,PreparedStatement.executeQuery 方法返回一个 ResultSet 对象。ResultSet 类包含在结果集中导航的方法和使用 ResultSet 更新数据的方法。

有关 ResultSet 对象的详细信息,请参见ResultSet 类

示例

在以下代码中,以 ResultSet 的形式访问查询的结果。在第一次指派时,ResultSet 会放置在第一行之前。然后,调用 ResultSet.moveFirst 方法来导航到结果集中的第一条记录。

var MyResultSet;
var PrepStmt;
PrepStmt = conn.prepareStatement("SELECT ID, Name FROM customer", null );
MyResultSet = PrepStmt.executeQuery( null );
MyResultSet.moveFirst();
示例

以下代码演示了如何获取当前行的列值。该示例使用字符数据,类似的方法也可用于其它数据类型。

getString 方法使用以下语法:MyResultSetName.getString( Index ),其中 Index 是 SELECT 语句中列名的序号位置。

if ( MyResultSet.getRowCount() == 0 ) {
} else {
  alert( MyResultSet.getString(1) );
  alert( MyResultSet.getString(2) );
  MyResultSet.moveRelative(0);
}

有关导航结果集的详细信息,请参见使用 SQL 导航

以下过程使用 SELECT 语句来检索数据库中的信息。查询的结果会被指派给 ResultSet 对象。

♦  执行 select 语句
  1. 声明 PreparedStatement 对象。

    var OrderStmt;
  2. 给您的 PreparedStatement 对象指派预准备语句。

    OrderStmt = Connection.prepareStatement( 
       "SELECT order_id, disc, quant, notes, status, c.cust_id, 
       cust_name, p.prod_id, prod_name, price 
      FROM ULOrder o, ULCustomer c, ULProduct p 
      WHERE o.cust_id = c.cust_id 
      AND o.prod_id = p.prod_id 
      ORDER BY der_id", "order_query_stmt" );

    第二个参数是一个永久名称,它为跨页 JavaScript 对象提供了持久性。

  3. 执行该查询。

    OrderResultSet = OrderStmt.executeQuery( "order_query" );

有关如何使用查询的详细信息,请参见 samples-dir\UltraLiteForMBusinessAnywhere\CustDB\custdb.js 中的 CustDB 示例代码。